trying to use ismember to find a row in a large matrix

11 views (last 30 days)
I have a matrix A that as three columns A= [1 2 0.5555; 1 2 0.4555; 1 1 0.5555] I have a matrix B that is 7000x3 I want to find all the indx of B where the first row of A, A(1,:) resides.
I tried using ismember and it only returns the indx of the last row it finds in B.
[tf,indx]=ismember(A(1,:),B,'rows')
tf =
1
indx =
3212
I want all the indx rows not just the last. Everything I've read about ismember suggests that this should give me all the indx. Can you tell me what I'm doing wrong? or propose an alternate solution? By the way I'm running ver. R2009a.
Thanks, John.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Apr 2013
No, check the R2009a ismember() documentation:
[tf, loc] = ismember(A, S, ...) returns an array loc containing the highest index in S for each element in A that is a member of S. For those elements of A that do not occur in S, ismember returns 0.
R2013a onward change the "highest" to "lowest"; see http://www.mathworks.com/help/matlab/ref/ismember.html and also the 'legacy' option there for that detail.
Either way, only a single value will be returned.
If you want all the indices, the trick is to reverse the order of the operands
tf = ismember(B, A(1,:), 'rows');
indx = find(tf);
Also, as per usual, be very cautious when comparing floating point values; see http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

More Answers (0)

Community Treasure Hunt

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

Start Hunting!