find index of nearest neighbor
Given a vector of sorted double values
vec of size
n and a vector of
m query objects
findNN determines for each element
the nearest neighbor index
o so that the following remains true:
there is no element
k is not
o so that
abs(vec[k] - q[i]) <
abs(vec[o] - q[i]).
a double vector which can be considered as query objects.
a sorted double vector which can be considered as a data base.
boolean enables test if
The internal algorithm of
findNN is implemented as binary search.
findNN has O(m * log(n)) time complexity.
Christian Panse 2007, 2008, 2009, 2010, 2012 based on the c stdlib bsearch methode and the R package:base function findInterval.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(NNidx <- findNN(q<-c(1, 1.0001, 1.24, 1.26), DB<-seq(1,5,by=0.25))) (NNidx == c(1,1,2,2)) DB<-sort(rnorm(100, mean=100, sd=10)) # should be 0 unique(DB[findNN(DB,DB)] - DB) q<-rnorm(100, mean=100) idx.NN<-findNN(q,DB) hist(DB[findNN(q,DB)] - q) # definition of findNN holds i<-1:5 findNN(3.5, i) i<-1:6 findNN(3.5, i)
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.