View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.5 | 13 ratings Rate this file 35 Downloads (last 30 days) File Size: 4.97 KB File ID: #8939 Version: 4.1
image thumbnail



Jos (10584) (view profile)


04 Nov 2005 (Updated )

NEARESTPOINT - find the nearest value in another vector

| Watch this File

File Information

NEARESTPOINT - find the nearest value in another vector
IND = NEARESTPOINT(X,Y) finds the value in Y which is the closest to each value in X, so that abs(Xi-Yk) => abs(Xi-Yj) when k is not equal to j.
IND contains the indices of each of these points.
  NEARESTPOINT([1 4 12],[0 3]) -> [1 2 2]
[IND,D] = ... also returns the absolute distances in D,that is D == abs(X - Y(IND))
NEARESTPOINT(X, Y, M) specifies the operation mode M:
1) 'nearest' : default, same as above
2) 'previous': find the points in Y that just precedes a point in X
NEARESTPOINT([1 4 12],[0 3],'previous') -> [1 1 1]
3) 'next' : find the points in Y that directly follow a point in X
NEARESTPOINT([1 4 12],[0 3],'next') -> [2 NaN NaN]
If there is no previous or next point in Y for a point X(i), IND(i)
will be NaN.
X and Y may be unsorted.
This function is quite fast, and especially suited for large arrays with
time data. For instance, X and Y may be the times of two separate events, like simple and complex spike data of a neurophysiological study.
Run NEARESTPOINT('test') to see a test of it's running time


This file inspired Efficient K Nearest Neighbor Search Using Jit, Ipdm: Inter Point Distance Matrix, Near2, and Find The Id Of The Closest Value In An Array To A Given Value.

MATLAB release MATLAB 8.3 (R2014a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
22 Feb 2017 actually

22 Mar 2016 vitstherm2

Thanks Julian, your code it is very interesting.
I have a question!
Which kind of distance do you use for your function?

Comment only
25 Jan 2016 Jos (10584)

Jos (10584) (view profile)

Thanks Julian, for pointing out this error. Fixed in version 4.1

Comment only
21 Jan 2016 Julian

Julian (view profile)

I found an error if input is:
>> [ind, d] = nearestpoint([4.5],[1 2 3 4],'next')
Error using -
Matrix dimensions must agree.

Error in nearestpoint (line 124)
D(q) = abs(x(q) - reshape(y(IND(q)),[],1)) ;

I fixed this by the condition that D should only be calculated if x(q) is not empty. Otherwise D = NaN.

if ~isempty(x(q))
D(q) = abs(x(q) - reshape(y(IND(q)),[],1)) ;
D = reshape(D(xi),sz) ;
D = NaN;

Does that make sense?

30 Mar 2015 LUI PAUL

24 Aug 2012 Drew Compston

Drew Compston (view profile)

I love this function and use it all the time. I did find two, possibly degenerate cases:

1. nearestpoint(1, []) errors at line 91.
2. nearestpoint([1 2], NaN) errors at line 116. This I fixed by changing that line to:
D(q) = abs(dx - reshape(y(IND(q)), size(dx))) ;

23 Feb 2009 Michael Ashby

Simple to use and understand. Thanks for the code and helpful documentation.

27 Jun 2007 Nick Clark

Nice, does the job

12 Apr 2007 Eduardo Gonzalez

Excellent, perfect for me.

26 Sep 2006 Drew Saunders

Perfect for my app!

19 Sep 2006 Scott Burnside

Nicely written. Quite fast. Thanks for the effort.

28 Feb 2006 David Wagener

Thanks this did exactly what I was looking for!!

01 Dec 2005 John D'Errico

Interesting submission. Careful coding and well

05 Nov 2005 urs (us) schwarz

yes, it is tuned to handle LARGE data sets as can be seen easily by slightly modifying the test suite: there are striking differences depending on the overall size of as well as the difference between the two vectors between this and the repmat approach
please note: line 114 crashes if(f) X is nx1 or 1xn and Y is 1x1 - someone ought to look into this... :-)
otherwise, a fine and useful piece of code

04 Nov 2005 Jos van der Geest

Thanks us, for sharing your thoughts.
<nearestpoint> will be especially usefull for _large_ datasets, in which repmat (twice) will cause memory problems.
For small datasets, either for-loops, or repmat will produce similar timings.
did you run nearestpoint('test')?

Comment only
04 Nov 2005 urs (us) schwarz

nice code, jos, albeit very long
however, how does your engine compare with our pedestrian one (error checks not shown):
% some data
a=1.1*[1 4 12 -11];
b=1.2*[0 -3 10 10 nan];
% the engine
% the result
as ever, just a thought

14 Sep 2012 1.1

Updated function to avoid errors when called with NaNs and empty variables. Thanks Drew.

25 Jan 2016 4.1

fixed error mentioned by Julian (D was requested but no nearest point was found).

Contact us