File Exchange

## nearestpoint(x, y, m)

version 4.2.0.0 (5.11 KB) by
NEARESTPOINT - find the nearest value in another vector

Updated 20 Dec 2017

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.
Example:
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

### Cite As

Jos (10584) (2021). nearestpoint(x, y, m) (https://www.mathworks.com/matlabcentral/fileexchange/8939-nearestpoint-x-y-m), MATLAB Central File Exchange. Retrieved .

Michal Kvasnicka

Michal Kvasnicka

OK ... just small typo in your function "testnearestpoint":

set(h(1), 'Xdata', DATA(:,1), 'Ydata', DATA(:, 2)) ;
set(h(2), 'Xdata', DATA(:,1), 'Ydata', DATA(:, 3)) ;

to

set(h(1), 'Xdata', log2(DATA(:,1)), 'Ydata', DATA(:, 2)) ;
set(h(2), 'Xdata', log2(DATA(:,1)), 'Ydata', DATA(:, 3)) ;

Now is computing power of your function obvious :)

Guy Van Hout

actually

vitstherm2

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

Jos (10584)

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

Julian

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) ;
else
D = NaN;
end

Does that make sense?

LUI PAUL

Drew Compston

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))) ;

Michael Ashby

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

Nick Clark

Nice, does the job

Eduardo Gonzalez

Excellent, perfect for me.

Drew Saunders

Perfect for my app!

Scott Burnside

Nicely written. Quite fast. Thanks for the effort.

David Wagener

Thanks this did exactly what I was looking for!!

John D'Errico

Interesting submission. Careful coding and well
documented.

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
us

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')?

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
[ix,ix]=min(abs(repmat(b.',1,size(a,2))-...
repmat(a,size(b,2),1)),[],2);
% the result
a,[b;ix.']
as ever, just a thought
us

##### MATLAB Release Compatibility
Created with R2017b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux