powered by
Given a vector of sorted double values vec of size n and a vector of m query objects q.
vec
n
m
q
findNN determines for each element q[i] in q the nearest neighbor index o so that the following remains true:
findNN
q[i]
o
there is no element k with 1 \(\le\) k \(\le\) n and k is not o so that
k
1
abs(vec[k] - q[i]) < abs(vec[o] - q[i]).
abs(vec[k] - q[i])
abs(vec[o] - q[i])
The internal algorithm of findNN is implemented as binary search. findNN has \(O(m * log(n))\) time complexity.
findNN(q, vec)
a double vector which can be considered as query objects.
a sorted double vector which can be considered as a data base.
an integer vector
protViz::findNN's cpluplus implementation.
protViz::findNN
# NOT RUN { (NNidx <- findNN(q<-c(1, 1.0001, 1.24, 1.26), DB<-seq(1,5,by=0.25))) (NNidx == c(1,1,2,2)) # should be 0 unique(DB[findNN(DB,DB)] - DB) # }
Run the code above in your browser using DataLab