Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all index using ismember

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

like image 567
Dark Avatar asked Jun 13 '26 05:06

Dark


2 Answers

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]

like image 65
Dan Avatar answered Jun 15 '26 03:06

Dan


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
like image 23
Shai Avatar answered Jun 15 '26 05:06

Shai