I was wondering if there are any clever tricks to search for sets of real
numbers in a larger list, especially without using a for loop. I only know
how to do the search for one element at a time.
A = [1.0, 2.0];
B = [1.0, 2.0, 3.0];
for i = 1:length(A)
[t, ind(i)] = min(abs(A(i)-B));
end
Just curious if there is a better way to write this code.
Thanks in advance,
Pete
Subject: Re: How to find a list of floats in another list of floats
"Peter de B. Harrington" <peter.harrington@ohio.edu> wrote in message
<47af565c$0$1103$4c368faf@roadrunner.com>...
> Hi:
>
> I was wondering if there are any clever tricks to search for sets of real
> numbers in a larger list, especially without using a for loop. I only know
> how to do the search for one element at a time.
>
> A = [1.0, 2.0];
> B = [1.0, 2.0, 3.0];
>
> for i = 1:length(A)
> [t, ind(i)] = min(abs(A(i)-B));
> end
>
> Just curious if there is a better way to write this code.
I'd compute an inter-point distance matrix,
limited by those points with a distance
less than some tolerance.
Simplest is to do this, assuming that both A
and B are row vectors...
d = abs(bsxfun(@minus,A',B));
% pick some appropriate tolerance
tol = 1e-12;
[i,j] = find(d < tol);
If your vectors are really long, say at least
a few thousand elements in each, then it gets
harder to do, since the intermediate distance
matrix will be a full matrix. There are still
solutions, but it takes a bit more work.
John
Subject: Re: How to find a list of floats in another list of floats
"Peter de B. Harrington" <peter.harrington@ohio.edu> wrote in message
<47af565c$0$1103$4c368faf@roadrunner.com>...
> Hi:
>
> I was wondering if there are any clever tricks to search for sets of real
> numbers in a larger list, especially without using a for loop. I only know
> how to do the search for one element at a time.
>
> A = [1.0, 2.0];
> B = [1.0, 2.0, 3.0];
>
> for i = 1:length(A)
> [t, ind(i)] = min(abs(A(i)-B));
> end
>
> Just curious if there is a better way to write this code.
>
> Thanks in advance,
>
> Pete
---------
Here is how I would have performed just the operation you described in your
for-loop construct using my antiquated version of matlab. I'm not at all sure
that it would be faster on a newer version, however.
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fonsi6$bp4$1@fred.mathworks.com>...
> "Peter de B. Harrington" <peter.harrington@ohio.edu> wrote in message
> <47af565c$0$1103$4c368faf@roadrunner.com>...
> > Hi:
> >
> > I was wondering if there are any clever tricks to search for sets of real
> > numbers in a larger list, especially without using a for loop. I only know
> > how to do the search for one element at a time.
> >
> > A = [1.0, 2.0];
> > B = [1.0, 2.0, 3.0];
> >
> > for i = 1:length(A)
> > [t, ind(i)] = min(abs(A(i)-B));
> > end
> >
> > Just curious if there is a better way to write this code.
> >
> > Thanks in advance,
> >
> > Pete
> ---------
> Here is how I would have performed just the operation you described in
your
> for-loop construct using my antiquated version of matlab. I'm not at all
sure
> that it would be faster on a newer version, however.
>
> [a,b] = meshgrid(A,B);
> [ignore,ind] = min(abs(a-b),[],1);
>
> Roger Stafford
As it turns out, using bsxfun can help quite
a bit here.
n = 1000;
A = rand(n,1);B = rand(n,1);
tic,[x,y] = meshgrid(A,B);d=abs(x-y);toc
Elapsed time is 0.221196 seconds.
d = abs(bsxfun(@minus,A,B'));toc
Elapsed time is 0.072752 seconds.
Unfortunately, bsxfun appeared only in the
last release or so. It never has to create
those intermediate arrays.
John
Subject: Re: How to find a list of floats in another list of floats
"John D'Errico" <woodchips@rochester.rr.com> wrote in message <font4t$ifr
$1@fred.mathworks.com>...
> As it turns out, using bsxfun can help quite
> a bit here.
>
> n = 1000;
> A = rand(n,1);B = rand(n,1);
>
> tic,[x,y] = meshgrid(A,B);d=abs(x-y);toc
> Elapsed time is 0.221196 seconds.
>
> d = abs(bsxfun(@minus,A,B'));toc
> Elapsed time is 0.072752 seconds.
>
> Unfortunately, bsxfun appeared only in the
> last release or so. It never has to create
> those intermediate arrays.
>
> John
--------
My matlab 4a (1994) is weeping, John!
Roger Stafford
Subject: Re: How to find a list of floats in another list of floats
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
wrote in message <fonupf$7fu$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in
message <font4t$ifr
> $1@fred.mathworks.com>...
> > As it turns out, using bsxfun can help quite
> > a bit here.
> >
> > n = 1000;
> > A = rand(n,1);B = rand(n,1);
> >
> > tic,[x,y] = meshgrid(A,B);d=abs(x-y);toc
> > Elapsed time is 0.221196 seconds.
> >
> > d = abs(bsxfun(@minus,A,B'));toc
> > Elapsed time is 0.072752 seconds.
> >
> > Unfortunately, bsxfun appeared only in the
> > last release or so. It never has to create
> > those intermediate arrays.
> >
> > John
> --------
> My matlab 4a (1994) is weeping, John!
>
> Roger Stafford
>
You might consider my NEARESTPOINT on the FEX:
A = rand(N,1) ; B = rand(N,1) ; tol = 1e-12 ;
tic ; [i,d] = nearestpoint(A,B) ; i = i(d<tol) ; toc ;
Some timing on my PC, Matlab 7.0
N = 1000 ;
Elapsed time is 0.004309 seconds.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.