From: Walter Roberson on
Jan Simon wrote:
> 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.

Is B a row vector? If so, then

Bidx = 1 : size(B,2);
for i = 1 : size(A,1)
C = any(bsxfun(@eq, A(i,:), B));
if any(C)
Clocs = Bidx(C);
[repmat(i,size(Clocs),1), Clocs(:)]
end
end
From: ruben.uma Rios on
Thank you all, for your contributions, but Yi Cao & Bruno Luong just
gave me the perfect solution, an easy-to-use one-line function

All the best,
Ruben

Walter Roberson <roberson(a)hushmail.com> wrote in message <htn0e4$f2t$1(a)canopus.cc.umanitoba.ca>...
> Jan Simon wrote:
> > 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.
>
> Is B a row vector? If so, then
>
> Bidx = 1 : size(B,2);
> for i = 1 : size(A,1)
> C = any(bsxfun(@eq, A(i,:), B));
> if any(C)
> Clocs = Bidx(C);
> [repmat(i,size(Clocs),1), Clocs(:)]
> end
> end