5.0

5.0 | 9 ratings Rate this file 110 downloads (last 30 days) File Size: 2.93 KB File ID: #8939

NEARESTPOINT

by Jos

 

04 Nov 2005 (Updated 01 May 2006)

Code covered by BSD License  

NEARESTPOINT - find the nearest value in another vector

Download Now | Watch this File

File Information
Description

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

Acknowledgements
This submission has inspired the following:
Efficient K-Nearest Neighbor Search using JIT, IPDM: Inter-Point Distance Matrix
MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
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
[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

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

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
us

01 Dec 2005 John D'Errico

Interesting submission. Careful coding and well
documented.

28 Feb 2006 David Wagener

Thanks this did exactly what I was looking for!!

19 Sep 2006 Scott Burnside

Nicely written. Quite fast. Thanks for the effort.

26 Sep 2006 Drew Saunders

Perfect for my app!

12 Apr 2007 Eduardo Gonzalez

Excellent, perfect for me.

27 Jun 2007 Nick Clark

Nice, does the job

23 Feb 2009 Michael Ashby

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

Please login to add a comment or rating.
Updates
01 May 2006

corrected error when using 'prev' option

Tag Activity for this File
Tag Applied By Date/Time
statistics Jos 22 Oct 2008 08:05:30
probability Jos 22 Oct 2008 08:05:30
sort Cristina McIntire 10 Dec 2008 16:53:16
distance Cristina McIntire 10 Dec 2008 16:53:16
find Cristina McIntire 10 Dec 2008 16:53:16
closest Cristina McIntire 10 Dec 2008 16:53:16
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com