|
Walter Roberson <roberson@hushmail.com> wrote in message <hujvdm$gu6$1@canopus.cc.umanitoba.ca>...
> Dave Brackett wrote:
> > Hi, I have the following code, but it runs quite slowly:
> >
> > for k=1:1000
> > a=find(and((b(:,1)==c(k)),(b(:,2)==d(k))));
> > end
> >
> > Is there a more efficient method?
>
> I will assume for this discussion that you do something with "a" inside the
> loop: if you do not, then "a" will be the same as if you had only done k=1000
>
> You could try the following for speed:
>
> T = bsxfun('eq', b(:,1), c) & bsxfun('eq', b(:,2), d);
> a = arrayfun(@(C) find(T(:,C)), 1:size(T,2), 'UniformOutput', 0);
>
>
> If I recall correctly, an approach some people have mentioned in the past is
>
> for k=1:1000
> a1 = find(b(:,1)==c(k));
> a = a1(b(a1,2)==d(k));
> end
>
> This is most efficient if the number of elements likely to be found is
> relatively low -- use the least likely test first so as to reduce the amount
> of testing needed for the more probable one.
>
> Would the values to be searched for happen to be non-negative integer values
> less than 256? If so, then there are tricks you can play using strmatch. If
> the values are non-negative integers that could be 256 to 65536, then there
> are tricks you can play using strfind.
Thanks. could you clarify what 'C' is in the arrayfun line that you suggest please? Also, to answer your questions: 1) I was writing to a file using an fprintf statement within the loop. 2) the values are unfortunately not integers and are not all less than 256.
Thanks again.
|