From: ruben.uma Rios on 27 May 2010 06:18 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 27 May 2010 06:55 "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 27 May 2010 06:58 You should take a look at function ISMEMBER(). Bruno
From: ruben.uma Rios on 27 May 2010 07:08 "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 27 May 2010 19:32 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
|
Next
|
Last
Pages: 1 2 Prev: tapping getframe for reduced computation time in image analysis Next: image processing toolbox |