Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to find a list of floats in another list of floats
Date: Sun, 10 Feb 2008 20:19:01 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 38
Message-ID: <fonm7l$8tn$1@fred.mathworks.com>
References: <47af565c$0$1103$4c368faf@roadrunner.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1202674741 9143 172.30.248.37 (10 Feb 2008 20:19:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 10 Feb 2008 20:19:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:450471


"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