Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find k-th nearest neighbor of a point in a set of point

I have a set of point (x,y) on a 2d plane. Given a point (x0,y0), and the number k, how to find the k-th nearest neighbor of (x0,x0) in the point set. In detail, the point set are represented by two array: x and y. The point (x0,y0) is given by the index i0. It means x0=x(i0) and y0=y(i0).

Is there any function or something in Matlab helps me this problem. If Matlab doesn't have such kind of function, can you suggest any other effective ways.

EDIT: I have to calculate this kind of distance for every point (x0,y0) in the set. The size of the set is about 1000. The value of k should be about sqrt(1500). The worst thing is that I do this many times. At each iteration, the set changes, and I calculate the distances again. So, the running time is a critical problem.

like image 979
opmfan Avatar asked Feb 22 '12 21:02

opmfan


2 Answers

if you will do this check for many points you might want to construct a inter-point distance table first

squareform(pdist([x y]))
like image 181
zamazalotta Avatar answered Nov 02 '22 01:11

zamazalotta


If you have the statistics toolbox, you can use the function knnsearch.

like image 27
3lectrologos Avatar answered Nov 02 '22 00:11

3lectrologos