File Exchange

image thumbnail


version 1.0 (96.1 KB) by

Computes nearest neighbour(s) by Euclidean distance



View License

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)

Comments and Ratings (7)

maram alfaraj

Daniel Lagos

Amazing code Richard!. I'm doing a simulation of a cristal lattice, How can i add PBC(periodic boundary conditions) into the code?.


sjye (view profile)


Chris (view profile)

cheers for the code, however just wondering a few things. in the program i am writing i have bacteria particles and fluid particles, with there initial coordinates in 2 seperate matrices. i need the neighbours lists for each point, both bacteria and fluid particles. i am using the radiusa round a point approach in the program

i can run the nearestneighbour file 4 different times and get bacteria/bacteria neighbours, fluid/fluid neighbours, fluid/bacteria neighbours and bacteria/fluid neighbours. this is very time consuming as well it always comes up that each particle is a neighbour of itself which is no good, just wondering if have any ideas about how to fix this, thanks, chris

je ciobi

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

Richard Brown

New version released which allows neighbour search within a radius

Please address bugs etc. to my email rather than here



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.



MathWorks update: Added Live Script.

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

Changed search keywords to accommodate American spelling

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

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

Tidied FEX Description

MATLAB Release
MATLAB 9.0 (R2016a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video