File Exchange

image thumbnail


version (96.1 KB) by Richard Brown
Computes nearest neighbour(s) by Euclidean distance


Updated 04 Mar 2016

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)

Cite As

Richard Brown (2021). nearestneighbour.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)

Michal Kvasnicka

knnsearch do mostly same job faster, more robustly and contains other distance metrics. Of course it requires Statistics toolbox...

Arthur de Jong

Barun Ghosh

Travis Yeager

Code is wonderful, how difficult would it be to add a minimum radius - maximum radius so that it could find all neighbors in a shell?

Hossein Zarei

Kim Mittendorf

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?.



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.

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!