dsearchn
Index of the nearest point in a set, for each query point.
Category: Mathematics > Computational Geometry
Syntax
k = dsearchn(P, PQ)
For each row of PQ, finds the closest row of P.
[k, d] = dsearchn(P, PQ)
Also returns the distances.
Description
The distance is the ordinary Euclidean distance. The search compares each query point against the data points, so the result is exact; if two data points are equally close, one of them is returned.
The function works in any dimension and is a quick way to snap points onto a mesh or a sample set.
Input arguments
P- Matrix of data points, one per row, with n columns for n dimensions.
PQ- Query points, one per row, with the same number of columns as
P.
Output arguments
k- Column vector with, for each query point, the row index in
Pof its nearest neighbor. d- Column vector of the Euclidean distances to those neighbors.
Example
Nearest of three points
P = [0 0; 5 5; 10 0];
PQ = [4 4; 9 1; 1 0];
[k, d] = dsearchn(P, PQ)
Output
k = 3×1
2
3
1
d = 3×1
1.4142
1.4142
1.0000