Nearest point search
Nearest 2-D Points
Create a matrix
P of 2-D data points and a matrix
PQ of 2-D query points. Find the nearest data point to each query point, and compute the corresponding distances.
rng default; P = rand([10 2]); PQ = [0.5 0.5; 0.1 0.7; 0.8 0.7]; [k,dist] = dsearchn(P,PQ);
Plot the data points and query points, and highlight the data point nearest to each query point.
plot(P(:,1),P(:,2),'ko') hold on plot(PQ(:,1),PQ(:,2),'*g') hold on plot(P(k,1),P(k,2),'*r') legend('Data Points','Query Points','Nearest Points','Location','sw')
Display the distances.
dist = 3×1 0.2349 0.2586 0.1825
P — Points
Points, specified as an m-by-n matrix
containing m points of dimension n. For example,
P = [0 0 0; 1 1 1] represents the 3-D coordinates for the points
(0,0,0) and (1,1,1).
PQ — Query points
Query points, specified as an r-by-n matrix
containing r points of dimension n. For example,
the 2-by-3 matrix
PQ = [-1 -1 -1; 2 2 2] represents the 3-D
coordinates for the two query points (-1,-1,-1) and (2,2,2).
The number of columns in
PQ must match the number of columns in
T — Delaunay triangulation
Delaunay triangulation, specified as a matrix returned by the
outind — Outside index value
Outside index value, specified as a scalar index value for query points outside of the convex hull.
outval is specified as
, then the output
k is equivalent to the syntax
k — Indices
Indices, returned as a column vector containing the indices of the data points
closest to the query points. The length of
k is equal to the number
of query points.
dist — Distance
Distance, returned as a column vector containing the Euclidean distances between
each query point and the closest input point. The length of
equal to the number of query points.
 Barber, C.B., D.P. Dobkin, and H.T. Huhdanpaa. “The Quickhull Algorithm for Convex Hulls.” ACM Transactions on Mathematical Software, Vol. 22, No. 4, Dec. 1996, p 469–483.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Introduced before R2006a