Compute Nearest Neighbors
Computes the nearest neighbor distances and indices of a sample using the infinite norm.
nearest_neighbors(data, k)
data
: Matrix of sample observations, each row is an observation.k
: Neighborhood order.List of distances and indices of the k-nearest neighbors of each point in data
.
Nearest neighbors are computed using the brute-force method.
X <- cbind(1:10) nearest_neighbors(X,3) set.seed(123) X <- cbind(runif(100),runif(100)) plot(X,pch=20) points(X[3,1],X[3,2],col='blue',pch=19, cex=1.5) nn <- nearest_neighbors(X,5) a = X[nn$nn_inds[3,-1],1] b = X[nn$nn_inds[3,-1],2] points(a,b,col='red',pch=19, cex=1.5)
Useful links