5.0

5.0 | 1 rating Rate this file 49 downloads (last 30 days) File Size: 28.69 KB File ID: #12574

nearestneighbour.m

by Richard Brown

 

08 Oct 2006 (Updated 07 Sep 2007)

No BSD License  

Computes nearest neighbour(s) by Euclidean distance

Download Now | Watch this File

File Information
Description

Compute nearest neighbours (by Euclidean distance) to a set of points of interest from a set of candidate points.

The points of interest can be specified as either a matrix of points (as columns) or indices into the matrix of candidate points.

Points can be of any (within reason) dimension.

nearestneighbour can be used to search for k nearest neighbours, or neighbours within some distance (or both)

If only 1 neighbour is required for each point of interest, nearestneighbour tests to see whether it would be faster to construct the Delaunay Triangulation (delaunayn) and use dsearchn to lookup the neighbours, and if so, automatically computes the neighbours this way. This means the fastest neighbour lookup method is always used.

A couple of examples:

% Candidate points
X = rand(2, 100);

% Points of interest
P = rand(2, 3);

% Find the nearest neighbour to each column of P
% where X(:, I(i)) is the neighbour to P(:,i)
I = nearestneighbour(P, X)

% Find the nearest 10 neighbours to each column of P
I = nearestneighbour(P, X, 'NumberOfNeighbours', 10)

% Find the nearest neighbours to the 2nd and 20th points in X
I = nearestneighbour([2 20], X)

% Find the neighbours in X which are within a radius of 0.2 from P
I = nearestneighbour(P, X, 'Radius', 0.2)

% Find the nearest neighbours to all columns of X
I = nearestneighbour(X)

Acknowledgements
This submission has inspired the following:
Kirchhoff Vortex Contour Dynamics Simulation, Efficient K-Nearest Neighbor Search using JIT, IPDM: Inter-Point Distance Matrix
MATLAB release MATLAB 7.3 (R2006b)
Other requirements Tested on Matlab 7.1 (R14SP3) It *should* work on R13 (untested) with minimal modification. The demo can only be published on Matlab 7.0 onwards. If it doesn't work in R13 try replacing all instances of True with 1, and all instances of False with 0.
Zip File Content  
Published M Files NEARESTNEIGHBOUR Demonstration
Other Files nearestneighbour.m,
demo/timingtest.m,
demo/html/nndemo.png,
demo/html/nndemo_01.png,
demo/nndemo.m,
demo/html/nndemo_03.png,
demo/html/nndemo_04.png,
demo/html/nndemo_02.png
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
11 Dec 2006 John D'Errico

Excellent on all counts. Good help, error checks, etc. Carefuly coded.

If the spelling bothers anyone, they can always write a synonym function. But with tab completion, why bother?

function [idx,tri] = nearestneighbor(varargin)
% Synonym for nearestneighbour
[idx,tri] = nearestneighbour(varargin{:});

A minor point - with only one argument, should nearestneighbour find nearest neighbors within columns of that array itelf? This is sometimes of interest. It currently produces an error. At the least an error check should catch this event.

Also, while I like the property/value pair interface, I'd suggest making it allow unambiguous shortenings of the property names. This way one would not need to type out the entire property name.

I'll look forward to see other metrics provided.

03 Sep 2007 Richard Brown

New version released which allows neighbour search within a radius

Please address bugs etc. to my email rather than here

Thanks,

Richard

19 Dec 2007 je ciobi

Does anyone knows how to compute the Gabriel Graph in Matlab?

Please login to add a comment or rating.
Updates
10 Oct 2006

Tidied FEX Description

20 Nov 2006

Fixed a bug which meant searching by index didn't work if the Delaunay Triangulation was used

13 Dec 2006

Thanks JD for some useful suggestions:
Added single input case. Now allows property names to be specified by an unambiguous case-insensitive shortening.
If a triangulation is supplied the program now automatically attempts to use it

29 May 2007

Changed search keywords to accommodate American spelling

29 Aug 2007

Added (finally) ability to search for neighbours within a certain radius

07 Sep 2007

Latest version contained a braindead while loop which made it very slow. Replaced this with fast vectorised code

Tag Activity for this File
Tag Applied By Date/Time
nearest Richard Brown 22 Oct 2008 08:43:05
neighbor Richard Brown 22 Oct 2008 08:43:05
neighbour Richard Brown 22 Oct 2008 08:43:05
closest Richard Brown 22 Oct 2008 08:43:05
delaunay Richard Brown 22 Oct 2008 08:43:05
dsearch Richard Brown 22 Oct 2008 08:43:05
dsearchn Richard Brown 22 Oct 2008 08:43:05
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com