| nnfind {nnclust} | R Documentation |
Find the nearest neighbours of points in one data set from another data set. Useful for Mallows-type distance metrics.
nnfind(from, to)
from |
A matrix giving the first sample, with rows specifying points and columns specifying dimensions. |
to |
Optional matrix with the same number of columns as from giving the second sample.
|
If to is specified, for each point in to find the nearest neighbour in from. If to is not specified, for each point in from find the nearest distinct neighbour in from.
The algorithm builds a k-d tree on from and drops points down the tree to find the nearest neighbour. This is much more efficient than a brute-force search as long as the dimension is low enough. For a million points in each data set and five dimensions, the code does only about 0.03% of the possible distance computations.
count |
Number of distance computations performed |
neighbour |
Vector of row numbers in from containing the nearest neighbour of each point in to |
dist |
Vector of distances to the nearest neighbour for each point in to |
data(faithful) nn<-nnfind(as.matrix(faithful)) plot(faithful) segments(faithful[,1], faithful[,2], faithful[nn$neighbour,1], faithful[nn$neighbour,2], col="blue")