Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to find a list of floats in another list of floats

Subject: How to find a list of floats in another list of floats

From: Peter de B. Harrington

Date: 10 Feb, 2008 19:54:00

Message: 1 of 8

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


Subject: Re: How to find a list of floats in another list of floats

From: John D'Errico

Date: 10 Feb, 2008 20:19:01

Message: 2 of 8

"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

From: Roger Stafford

Date: 10 Feb, 2008 22:07:02

Message: 3 of 8

"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


Subject: Re: How to find a list of floats in another list of floats

From: John D'Errico

Date: 10 Feb, 2008 22:17:01

Message: 4 of 8

"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

From: Roger Stafford

Date: 10 Feb, 2008 22:45:03

Message: 5 of 8

"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

From: Jos

Date: 11 Feb, 2008 10:37:02

Message: 6 of 8

"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.

N = 1e6 ;
Elapsed time is 4.651996 seconds.

Nearestpoint can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8939&objectType=FILE

hth
Jos

Subject: Re: How to find a list of floats in another list of floats

From: us

Date: 11 Feb, 2008 11:23:01

Message: 7 of 8

"Peter de B. Harrington":
<SNIP lost his/her numbers...

one of the other solutions

% the data
     a=pi*(1:10);
     b=pi*(11:20);
     b([2,4,5,6])=a(1:4);
     b(1)=3;
% the engine
     tol=.2;
     as=sort([a-tol,a,a+tol]);
     [ix,ix]=histc(b,as);
     r=b(ix>0);
% the results
     find(ix>0)
% 1 2 4 5 6
     r
% 3 3.14159 6.28319 9.42478 12.5664

us

Subject: Re: How to find a list of floats in another list of floats

From: Bruno Luong

Date: 11 Feb, 2008 11:39:02

Message: 8 of 8

One among many solutions:

A=rand(1,3);
B=rand(1,50);

[Bsorted I J]=unique(B);
inearest=I(interp1(Bsorted,(1:length(Bsorted)),A,...
           'nearest','extrap'));

A - B(inearest)

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
sort us 11 Feb, 2008 06:24:59
histc us 11 Feb, 2008 06:24:59
code us 11 Feb, 2008 06:24:59
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics