From: Joel on
I wantto make this calculation as fast as possible for large data sets:
I have a set of points in dimension N.
I generate a new point and I want to find the which of original M points it is closest to.
Without resorting to quad-trees, bounding boxes or other specialized data structures, what do you think is the fastest (vectrozed) way to write this code?

Here is what I have now:
%example data
N=4; NumPts = 100000;
Pts = rand(NumPts,N);
NewPt = rand(1,N);
% Compute closest point
tic
dist = zeros(NumPts,1)
for i = 1:N
dist = dist + (Pts(:,i)-NewPt(1,i)).^2
end
[dmin,i] = min(dist);
closestPt = Pts(i,:)
toc

I have tried a few approaches, but so far this is my best. Any ideas on how to improve? Are there any built in function I can take Advantage of?

Right now I have examples with a million points in 8 dimensions but I can't get much beyone because this calculation is the bottleneck..
From: Jan Simon on
Dear Joel!

> I generate a new point and I want to find the which of original M points it is closest to.

> %example data
> N=4; NumPts = 100000;
> Pts = rand(NumPts,N);
> NewPt = rand(1,N);
> % Compute closest point
> tic
> dist = zeros(NumPts,1)
> for i = 1:N
> dist = dist + (Pts(:,i)-NewPt(1,i)).^2
> end
> [dmin,i] = min(dist);
> closestPt = Pts(i,:)
> toc

Did you try this instead of the loop:
dist = sum(bsxfun(@minus, Pts, NewPt) .^2, 2);

Good luck, Jan
From: Joel on
Jan, Thanks so much for the tip. Its very slick, never saw that function before.
However that actually takes about twice as long on my machine.

I shoudl add that, I thought a bit about datatype. I am willing to use single to go faster.
From: Matt J on
"Joel" <esposito(a)usna.edu> wrote in message <hljh3s$shr$1(a)fred.mathworks.com>...
> Jan, Thanks so much for the tip. Its very slick, never saw that function before.
> However that actually takes about twice as long on my machine.
>
> I shoudl add that, I thought a bit about datatype. I am willing to use single to go faster.
=======================

That's probably because you're running with small N. For larger N, bsxfun will be faster. If large N is not relevant to you, I too have found that a for-loop is the fastest solution.
From: Joel on
I used 1 million points and N=20 in my testing.

Any other possible ideas?