So say I have two arrays:
A:14 63 13
38 44 23
11 12 13
38 44 23
B:38 44 23
I am trying to use ismember to return the index of every location where B is found in A. All examples I have found online only list the first or last occurrence of a match, I am trying to have a list indices for all values that match, even repeating ones. Thanks
Use ismember with the 'rows' arugment:
ismember(A, B, 'rows')
which results in a logical array [0 1 0 1] which is often better than an array of indices but if you want the indices specifically then just use find:
find(ismember(A,B,'rows'))
to return [2,4]
Note that this method will still work if B has multiple rows e.g. B = [38 44 23; 11 12 13], it will return [0; 1; 1; 1]
You can use bsxfun for the comarison:
idx = find( all( bsxfun(@eq, A, B), 2 )); %// only where all line matches
Results with
idx =
2
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With