5.0

5.0 | 5 ratings Rate this file 342 downloads (last 30 days) File Size: 25.09 KB File ID: #22190

FAST K-NEAREST NEIGHBORS SEARCH

by Luigi Giaccari

 

20 Nov 2008 (Updated 23 Sep 2009)

Code covered by BSD License  

Simple but very fast algorithm for nearest neighbors search. Supports KNN and radius search.

Download Now | Watch this File

File Information
Description

You can find the description at:

http://www.advancedmcode.org/gltree.html

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
K-NEAREST NEIGHBOURS AND RADIUS (RANGE) SEARCH
This submission has inspired the following:
FAST K-NEAREST NEIGHBOURS SEARCH 3D VERSION, K-NEAREST NEIGHBOURS AND RADIUS (RANGE) SEARCH

MATLAB release MATLAB 7.5 (R2007b)
Other requirements Need a mex compiler
Zip File Content  
Other Files
GLTree230909/BuildGLTree.cpp,
GLTree230909/BuildGLTree.m,
GLTree230909/BuildGLTree.mexw32,
GLTree230909/DeleteGLTree.cpp,
GLTree230909/DeleteGLTree.m,
GLTree230909/DeleteGLTree.mexw32,
GLTree230909/GLTree.cpp,
GLTree230909/GLTree.h,
GLTree230909/KNNSearch.cpp,
GLTree230909/KNNSearch.m,
GLTree230909/KNNSearch.mexw32,
GLTree230909/NNSearch.cpp,
GLTree230909/NNSearch.m,
GLTree230909/NNSearch.mexw32,
GLTree230909/RSearch.cpp,
GLTree230909/RSearch.m,
GLTree230909/RSearch.mexw32,
GLTree230909/TestMexFiles.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
24 Nov 2008 matt dash

I get an error when running "mex GlTreeMex.cpp" with
"Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2007b\sys\lcc"
as my compiler.

24 Nov 2008 Luigi Giaccari

I don't know what can it be, I used Visual c++ 6.0,
  if you want an already compiled win32 mex you can contact me on my e-mail (Matlab exchange stuff do not axcept any more mex files, I can even send you the whole test with ann library).

I am sorry, with other files I have had this mex problem too.

28 Nov 2008 a

1) using the data provided in the demo file, ANN library took 0.02 seconds; this code took 0.06 seconds; for N=1e7, this code was 10 times slower than ANN library. Please be more specific when saying "In many cases is faster than ANN library". Also, ANN supports more options (arbitrary dimensions, etc).
2) to make it compile, I had to fix the following:
a) replace the mexFunction line in GLtreeMex.cpp by:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]){
b) replace mex GlTreeMex.cpp with mex GLtreeMex.cpp
c) replace NNG=GLTreeMex(px,py,qpx,qpy); by:
NNG=GLtreeMex(px,py,qpx,qpy);

29 Nov 2008 Luigi Giaccari

1)I don't Know what kind of data you used but I didn't provide any.
The demo file contains just random points generation.
My pc do not support 1e7 due to help memory error, anyway finding the NNG in 1e7 points take much longer than 0.02 sec for both algorithoms.
My algorithom suffers query points outside the bounding box of reference, this should be the case in which ANN is faster.
2)One thing you are right is that ANN is more flexible.
3)From your comment it is not clear what you changed seems just like you copied and pasted the same line.
4)you could at least use your name
5)If you have problems my e-mail is the best place where fix them so please avoid fake comments

06 Dec 2008 a

1) Great code, works as advertised ! I would correct my initial rating to 5 stars if mathworks allowed that (there was probably a bug in my previous timing)
the speedup over ann library is quite impressive, for 2d inputs and k=1 nearest neighbor. For N=1e7, it ran in 20 sec instead of 80 for ann)

2) the changes I had to do to run the code are for compatibility with non-windows systems:
void mexFunction( int nlhs, const mxArray *plhs[1],
                  int nrhs, const mxArray *prhs[4])
changed to:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
(note the prhs[4], etc)

and case-sensitivity:
mex GlTreeMex.cpp => mex GLtreeMex.cpp
NNG=GLTreeMex(px,py,qpx,qpy); =>
NNG=GLtreeMex(px,py,qpx,qpy);
(note the case difference)

07 Dec 2008 Michael Jordan

Excellent code. I am looking forward to your extension to 3D, k-neighbour and range search, which are more useful functions than the current one.

06 Jan 2009 James Tursa

To Matt Dash: lcc is a C compiler, not a C++ compiler, so you should not expect lcc to compile cpp files.

20 Mar 2009 Johannes Korsawe

Really good! Great work and just came in handy.
Thanks a lot for sharing your work!

22 May 2009 Bruno Luong

Impressive running speed. I have tested against Matlab Delaunay/dsearch nearest point for 2 x 10^5 points and the code won hand down in speed. Very good work. I would like to see the author to provide mean/worse complexity of his algorithm, which is always necessary step when integrating in reliable software project.

23 May 2009 Luigi Giaccari

Given:
 
 N=reference +query points

Complexity of the algorithm is linear for random uniformly spaced points. Both for the serach and the tree construction.

 It becomes N^2 (Brute search) for very sparse dataset.

However this patological case can be detected after 5-10% running time. In this case is better to switch to kd-tree data structure. ( this detection is not present in this version)

                    Luigi

21 Oct 2009 Andrew

When I run mex DeleteGLTree.cpp on version R2009a with Mac OS 10.6 (Snow Leopard), I get the following message:

Undefined symbols:
  "_mexFunction", referenced from:
     -exported_symbols_list command line option
ld: symbol(s) not found
collect2: ld returned 1 exit status

    mex: link of ' "DeleteGLTree.mexmaci"' failed.

This turns out to be a problem with the declaration of mexFunction. For the solution, see
http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/173095

03 Nov 2009 Luigi Giaccari

Thank you it will fixed in the next release.
Please report comments on :
http://www.advancedmcode.org/
I don't check my FEX profile very often.

Thank you again

 Luigi

Please login to add a comment or rating.
Updates
29 Nov 2008

Fixed a little the demo file

01 Dec 2008

Fixed help lines

08 Dec 2008

Added Build Tree, NNSearch, RSearch, KSearch

17 Dec 2008

Modified presentation

02 Jan 2009

Fixed a bug on KNN, now it is possible to return the distances

23 Mar 2009

Updated presentation

21 May 2009

Changed a capital letter that caused compiling problem on linux compiler

22 May 2009

Back to previous version, I messed up something in the last update

20 Jun 2009

Updated Presentation

23 Jun 2009

Updated presentation

30 Jun 2009

Changed a link

25 Aug 2009

Added link

27 Aug 2009

Added link

23 Sep 2009

changed presentation

Tag Activity for this File
Tag Applied By Date/Time
kd tree Luigi Giaccari 20 Nov 2008 16:07:51
search Luigi Giaccari 20 Nov 2008 16:07:51
optimization Luigi Giaccari 20 Nov 2008 16:07:51
image compression Luigi Giaccari 20 Nov 2008 16:07:51
nearest Luigi Giaccari 20 Nov 2008 16:07:51
neighbor Luigi Giaccari 20 Nov 2008 16:07:51
closest Luigi Giaccari 20 Nov 2008 16:07:51
nng Luigi Giaccari 20 Nov 2008 16:07:51
tree Luigi Giaccari 20 Nov 2008 16:07:51
ann Luigi Giaccari 20 Nov 2008 16:07:51
graph Luigi Giaccari 20 Nov 2008 16:07:51
image Luigi Giaccari 08 Dec 2008 16:24:58
compression Luigi Giaccari 08 Dec 2008 16:24:58
range Luigi Giaccari 08 Dec 2008 16:24:58
kd Luigi Giaccari 08 Dec 2008 16:24:58
optimization Cristina McIntire 12 Dec 2008 15:36:02
image Cristina McIntire 12 Dec 2008 15:36:02
aerospace Luigi Giaccari 23 Mar 2009 14:09:55
3d Luigi Giaccari 23 Mar 2009 14:09:55
points Luigi Giaccari 23 Mar 2009 14:09:55
graphics Luigi Giaccari 23 Mar 2009 14:09:55
m Luigi Giaccari 23 Mar 2009 14:09:55
automotive Luigi Giaccari 23 Mar 2009 14:09:55
communications Luigi Giaccari 23 Mar 2009 14:09:55
control design Luigi Giaccari 23 Mar 2009 14:09:55
data export Luigi Giaccari 23 Mar 2009 14:09:55
data import Luigi Giaccari 23 Mar 2009 14:09:55
image processing Luigi Giaccari 23 Mar 2009 14:09:55
2d Luigi Giaccari 23 Mar 2009 14:09:55
kdtree Luigi Giaccari 28 Mar 2009 04:29:14
mex Luigi Giaccari 28 Mar 2009 04:29:15
3d Sebastiano 19 Jun 2009 11:17:47
range yasmin begum 19 Sep 2009 01:15:16
points yasmin begum 19 Sep 2009 01:15:35
aerospace yasmin begum 19 Sep 2009 01:15:54
 

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