function 'nearest' vs 'knnsearch'

3 views (last 30 days)
Viktoria Cs.
Viktoria Cs. on 5 Aug 2017
Edited: John D'Errico on 5 Aug 2017
Hi. I am confused and need some help regarding the function 'nearest'. I have used this several times to find the nearest points in vector A compared to vector B. For instance, in codes like this: for m = 1:length(evtimes) timeindex(m,1) = nearest(bufftime, evtimes(m,1)) end where both bufftime and evtimes are row vectors, and I am looking for the points in bufftime that are closest to evtimes. I just ran a long series of these the other day. However, matlab now gives an error message, saying 'too many input arguments'. Now, I understand that nearest(_) also exists, and I can also use it for graphs and nodes, but that's not what I wanted here. And as I say, this worked fine a few days ago. Now I can only use knnsearch(X,Y), which is fine, I compared the results I am now getting with knnsearch with the ones I got with nearest the other day. But I just don't understand why nearest isn't working now... Please explain it to me... Is it possible that matlab has changed the way 'nearest' works???
thanks!

Answers (1)

John D'Errico
John D'Errico on 5 Aug 2017
Edited: John D'Errico on 5 Aug 2017
At the command line, type this:
which nearest -all
In my version of MATLAB, it finds TWO such function, both of which lie below the graphfun directory, so are methods for graph and digraph objects.
On your computer, I predict that you have written a script called nearest.m, then saved it on your search path. DON'T DO THAT!!!!!!!!
To fix the problem, name it something else. And avoid naming scripts with the names of useful functions in the future.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!