From: ruben.uma Rios on
I would like to know if there is a simple and efficient way (i.e. without using several loops) to find which elements of an array appear in another array. For instance:

A = [2157 2206 2208 2257;
1 4 2 51]

B = [2036 2264 2008 51 36]


I would like to know which elements of B appear in A.

I have written this code (which seems to work) but I would like to know if there is a more efficient way to do it

for i=1:1:size(A,1)
for j=1:1:size(B,2)
C = bsxfun(@eq, A(i,:), B(j));
S = find(C);
if (size(S) > 0) [i,j] end
end
end


Any suggestion is welcomed :)
From: Yi Cao on
"ruben.uma Rios" <rubenysuifon(a)hotmail.com> wrote in message <htlgsu$g9c$1(a)fred.mathworks.com>...
> I would like to know if there is a simple and efficient way (i.e. without using several loops) to find which elements of an array appear in another array. For instance:
>
> A = [2157 2206 2208 2257;
> 1 4 2 51]
>
> B = [2036 2264 2008 51 36]
>
>
> I would like to know which elements of B appear in A.
>
> I have written this code (which seems to work) but I would like to know if there is a more efficient way to do it
>
> for i=1:1:size(A,1)
> for j=1:1:size(B,2)
> C = bsxfun(@eq, A(i,:), B(j));
> S = find(C);
> if (size(S) > 0) [i,j] end
> end
> end
>
>
> Any suggestion is welcomed :)

ismember(B,A)

HTH
Yi
From: Bruno Luong on
You should take a look at function ISMEMBER().

Bruno
From: ruben.uma Rios on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <htlj80$icq$1(a)fred.mathworks.com>...
> You should take a look at function ISMEMBER().
>
> Bruno

Thank you very much Yi and Bruno, that is exactly what I was looking for !!!

All the best
From: Jan Simon on
Dear Ruben

> I have written this code (which seems to work) but I would like to know if there is a more efficient way to do it
>
> for i=1:1:size(A,1)
> for j=1:1:size(B,2)
> C = bsxfun(@eq, A(i,:), B(j));
> S = find(C);
> if (size(S) > 0) [i,j] end
> end
> end

sizeB2 = size(B, 2); % Do not call SIZE repeatedly
for i=1:size(A,1)
for j=1:sizeB2
if any(A(i, :) == B(j))
[i,j]
end
end
end

size(find(bsxfun(@eq, X, y)) is much slower than any(X==y), if y is a scalar.

Jan