Main Content

nearestNeighbor

(Not recommended) Vertex closest to specified point

nearestNeighbor(DelaunayTri) is not recommended. Use nearestNeighbor(triangulation) instead.

DelaunayTri is not recommended. Use delaunayTriangulation instead.

Description

PI = nearestNeighbor(DT,Q) returns the index of the nearest point in DT.X for each query point location in matrix Q.

Note

nearestNeighbor is not supported for 2-D triangulations that have constrained edges.

example

PI = nearestNeighbor(DT,QX,QY) and PI = nearestNeighbor(DT,QX,QY,QZ) allow the query points to be specified in column vector format when working in 2-D and 3-D.

[PI,D] = nearestNeighbor(___) returns the corresponding Euclidean distances between the query points and their nearest neighbors in D.

Examples

collapse all

Create a Delaunay triangulation for a set of random points.

rng default
x = rand(10,1);
y = rand(10,1);
dt = DelaunayTri(x,y);

Create a matrix of 2-D query points.

Q = [0.25 0.25; 0.5 0.5];

Find the nearest neighbors to the query points.

pid = nearestNeighbor(dt,Q)
pid = 2×1

     7
     7

Input Arguments

collapse all

Delaunay triangulation representation, specified as a DelaunayTri object.

Query points, specified as a matrix. Q is of size mpts-by-ndim, where mpts is the number of query points and ndim the dimension of the space where the points reside.

Query points, specified as separate column vectors. Specify QX and QY to give the coordinates of 2-D query points, or specify QX, QY, and QZ to give the coordinates of 3-D query points.

Output Arguments

collapse all

Point indices of nearest neighbor, returned as a column vector. PI is a vector of point indices that index into the points DT.X. The length of PI is equal to the number of query points mpts

Euclidean distances, returned as a column vector. Each element in D gives the value of the Euclidean distance between a query point and its nearest neighbor..

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2009a