# If I have an array and I need to find an element in the array how do I go about it?

29 visualizzazioni (ultimi 30 giorni)
Joseph il 28 Mag 2024 alle 13:46
Commentato: William Rose il 28 Mag 2024 alle 14:22
say states,
states = [1 0 0;
1 1 0
0 1 0
0 1 1
0 0 1
1 0 1
1 1 1];
C = 110;
How do I find the index of C in states and call it?
Ideally C is 2 then states(2) will give me 110 what key word can I use? or How do I go about it?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

John D'Errico il 28 Mag 2024 alle 14:05
Modificato: John D'Errico il 28 Mag 2024 alle 14:06
I think you are confusing things, not understanding there is a difference between the number 110, and the vector [1 1 0], and then of the rows of an array. This is not a question of keywords, but a question of understanding what a variable contains, and how numbers are stored.
states = [1 0 0;
1 1 0
0 1 0
0 1 1
0 0 1
1 0 1
1 1 1];
States is a 2 dimensional array. You can access the second ROW of states as:
states(2,:)
ans = 1x3
1 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now, if you want to identify which row of states matches the vector
C = [1 1 0]
C = 1x3
1 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
then you could do something like
ismember(states,C,'rows')
ans = 7x1 logical array
0 1 0 0 0 0 0
Ismemmber returns a boolean vector that tell you the set of all rows of states which match your target (in case there was more than one row.) Or you could tell find to return only the first row. So then you could do this:
find(ismember(states,C,'rows'),1,'first')
ans = 2
So find only 1 result, and make it the first row it finds, in case of multiple positive results.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Joseph il 28 Mag 2024 alle 14:14
Thanks for the clarification, It works

Accedi per commentare.

### Più risposte (2)

Torsten il 28 Mag 2024 alle 14:05
Spostato: Torsten il 28 Mag 2024 alle 14:05
Compute
100*states(:,1)+10*states(:,2)+states(:,3)
and use "find" to compare with C.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 28 Mag 2024 alle 14:21
Also using matrix multiplication:
states = [1,0,0; 1,1,0; 0,1,0; 0,1,1; 0,0,1; 1,0,1; 1 1 1];
C = 110;
X = find(C==states*[100;10;1])
X = 2

Accedi per commentare.

William Rose il 28 Mag 2024 alle 14:12
states = [1 0 0; 1 1 0; 0 1 0; 0 1 1; 0 0 1; 1 0 1; 1 1 1];
C = [1 1 0];
for i=1:length(states), if states(i,:)==C, idx=i; end, end
fprintf('States=C at row %d.\n',idx)
States=C at row 2.
Not elegant, but it works.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
William Rose il 28 Mag 2024 alle 14:22
If there are multiple matching rows, the code above will report the last row only. In order to get all rows that match, you could do
states = [1 0 0; 1 1 0; 0 1 0; 0 1 1; 0 0 1; 1 0 1; 1 1 1; 1 1 0];
C = [1 1 0]; % states==C at rows 2 and 8
idx=[];
for i=1:length(states), if states(i,:)==C, idx=[idx,i]; end, end
disp(idx)
2 8

Accedi per commentare.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by