Code covered by the BSD License  

Highlights from
kd-tree for matlab

4.86667

4.9 | 15 ratings Rate this file 127 Downloads (last 30 days) File Size: 129 KB File ID: #21512
image thumbnail

kd-tree for matlab

by

 

22 Sep 2008 (Updated )

A kd-tree mex lib which allows for nearest neighbor, k-nearest neighbor, range and ball queries

| Watch this File

File Information
Description

kdtree provides a minimalistic implementation of kd-tree. The implementation can be used either inside MATLAB by means of MEX calls, or as a standalone tool, directly from a C/C++ program. The image on the website has been creaed with "fulltest.m"

This implementation offers the following functionalities:
- kdtree_build: k-d tree construction O( n log^2(n) )
- kdtree_delete: frees memory allocated by kdtree
- kdtree_nearest_neighbor: nearest neighbor query (for one or more points)
- kdtree_k_nearest_neighbors: kNN for a single query point
- kdtree_range_query: rectangular range query
- kdtree_ball_query: queries samples withing distance delta from a point

For more information, please refer to the following page:
https://sites.google.com/site/andreatagliasacchi/software/matlabkd-treelibrary

Acknowledgements

K D Tree inspired this file.

This file inspired Kdtree Implementation In Matlab.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Requires a working MEX compiler. Runs on all platforms.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (34)
01 May 2014 Antonio

Hi, thanks for sharing the code.

I found a strange behavior from the computational execution time:

kdtree_k_nearest_neighbors(tree,P,1);

is far way faster than:

kdtree_nearest_neighbor(tree,P);

being P a point with 25 dimensions.

16 Jan 2014 Christos  
28 Oct 2013 BESMA

I tried to execute this code but i didn't undrestand the steps to do this!! ireally need it any help and thank you.

07 Oct 2013 tarek zougari

great work & thx another time Andrea

am posting this for newbies next readers

1) install a compiler (u have to respect the order ) see here : http://www.mathworks.com/support/solutions/en/data/1-ECUGQX/

2) u have to be in matlab on the folder where the files are, see here :
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316917

:)

04 Oct 2013 Andrea Tagliasacchi

Tarek, that just means you haven't compiled the library. @see kdtree_build

04 Oct 2013 tarek zougari

Gracie mille Andrea for answering :)
i tried : mex kdtree_build.cpp

the msg i have is :

mex kdtree_build.ccp

" C:\PROGRA~1\MATLAB~1\R2013A\BIN\MEX.PL: Error: 'kdtree_build.ccp' not found.

Error using mex (line 206)
Unable to complete successfully."

ps : i did set the path

14 Sep 2013 Eran

Great!
One thing that is very important and missing is a way to save and load the tree to/from disk.
It takes ~1000s to generate a tree for
a 10^6 X 3 matrix. It will be useful if you can do it only once and load it from
hard drive in a fraction of a second.

15 Jun 2013 Lingji Chen

Great work! Thank you!

A question: I inferred this from your demo, but wanted to confirm with you: The index returned by a query is guaranteed to correspond to the row index of the data used to create the kd-tree, correct?

22 Mar 2013 Philip Du Toit

Thank you! Gave my application 100x speedup.

07 Aug 2012 kenza

hi every body
it's the first time who i participate in this blog, I am a beginner in matlab programmation. Actually, I try understand the K-d tree code. So i found a matlab sript for the implementing of algorithm. I give you the link to consult the sript code. My question is connected to the input of kdree code especially 'val'. What mean 'val' ? is it a matrix or array and how can i obtain it or calculate it?
I need your help. your answers can give me another reflexion.

http://senlinhou.wordpress.com/2010/09/26/scattered-interpolation-in-n-dimensions/#comment-65

15 Jun 2012 Andrea Tagliasacchi

Sorry for the butchered english in my last message ^_^

14 Jun 2012 Andrea Tagliasacchi

Be advised.. The library has been subject to large updates and it's in Beta. It's much faster and it offers a matlab class wrapper. The updated library can be found at the following URL:
(Includes 64bit binaries for OSX and WIN7)

http://code.google.com/p/kdtree-matlab/

04 Jun 2012 Andrea Tagliasacchi

@Jose, yah the tree is static.. sorry
@Bryan, that's a good question :) I am working on the library these days, I will try to answer it.

04 Jun 2012 Bryan

Great work! Performance Question: Why is kdtree_nearest_neighbor() slower than kdtree_k_nearest_neighbors(...,1)? In my testing with the same set of 2000 4-dimensional points, k_nearest is 4x faster.

10 Jan 2012 Jose

Great work! I'm using it in my thesis project and it works well. But I don't know how I can delete nodes in the tree meanwhile keep the tree balanced.

21 Jun 2011 Prateek Rungta

Hi Andrea,
Thanks for the file, great work. Just a FYI: the tree doesn't appear to be correctly created in the event there are nan values in the data-set. In my tests, the points need to be at least 50-60% non-nan for the tree to work as expected.

17 Apr 2011 Duy Nguyen

Hello Mr.Andrea Tagliasacchi, your code is really great but i need some other things. I wonder how i can make the function nearest neighborhood get distances between corresponding pairs. I mean that function's output return not only indexes but also the distances. Thank you in advance for your time

21 Apr 2010 Andrea Tagliasacchi

Hello Eval, that's a completely different problem, the underlying structure of this kd-tree is built to exploit the complete balancedness of the data. If you need a dynamic kd-tree then you would need to code it differently. By the way, why kd-tree? They are not exactly the best for this kind of task. Have you ever thought about using quad or octrees? Well, unless you have very high dimensional data...

20 Apr 2010 Eval Bladimir Bacca Cortes

It works really nice, but I need something and I don't know how to do it efficiently. I need to update an existing kd-tree with new data, but without losing the actual indexes. Is that possible? Please could you help me? Thanks in advance for your time....

15 Apr 2010 Georgios Evangelidis

Very nice and complete work! It worked perfectly. VC Express Ed. 2008 works fine. Thanks a lot!.

10 Feb 2010 Andrea Tagliasacchi

The easiest solution would be enclosing the methods in a Matlab class and let Matlab call the "delete" functions automatically.

I might work on it in the future and improve it to include this functionality as well. I suggest you to be more careful with your de-allocations in the meantime :)

10 Feb 2010 Alastair Harrison

This is a really nice implementation. It's worked very well for me and is much faster than other implementations I've tried.

If I accidentally call kdtree_delete twice on the same handle, matlab quits immediately. Would it be possible for the library to maintain an internal list of handles to avoid this problem?

It would also allow implementation of a 'kdtree_delete_all' function, to free up all trees that have been created in a session. That's useful because if a piece of matlab code crashes before it gets to kdtree_delete then you can end up with an orphaned tree.

It would be nice to have a 'kdtree_delete_all' function to free up all trees that have been created. Of course, this would require a list of trees to be maintained within the library.

26 Oct 2009 Dominic

Ok, Shame on me, there is a method

"kdtree_delete"

that needs to be called for releasing the memory again.

22 Oct 2009 Dominic

On my System (64bit Ubuntu, Matlab R2009a) there seems to be a memory leak with this implementation:

for i = 1:1000
tree = kdtree_build(rand(1000,1));
end

grabs around 140MB of system memory, according to top. That is never released again. Do I have to release the resources myself in some way or might this be a bug?
I really depend on this implementation, so you would do me a great favour by having a look at it...

15 Oct 2009 Dominic

Thanks a lot for this contribution - I've been using it a lot recently and it just works perfectly :)

09 Sep 2009 sun jin

kdtree_build.cpp
D:\kdtree1.2\kdtree\KDTree.h(144) : error C2374: 'dIdx' : redefinition; multiple initialization
D:\kdtree1.2\kdtree\KDTree.h(137) : see declaration of 'dIdx'
D:\kdtree1.2\kdtree\KDTree.h(225) : error C2371: 'i' : redefinition; different basic types
D:\kdtree1.2\kdtree\KDTree.h(223) : see declaration of 'i'

D:\MATLAB\R2008A\BIN\MEX.PL: Error: Compile of 'kdtree_build.cpp' failed.

??? Error using ==> mex at 207
Unable to complete successfully.

27 Aug 2009 lin ming

great work!
note: compile with visual c++ 6.0 will cause fatal error on my computer. However, VS2008 works fine.

17 May 2009 Florian Brucker

Worked perfectly out of the box after compiling in MATLAB as specified by the docs. Thanks a lot!

One little thing: The help for kdtree_k_nearest_neighbors says that the neighbors are returned with increasing distance order. This seems to be incorrect, my experiments tell me they are returned in decreasing distance order (e.g. if the point itself is part of the tree, then it is returned as the last of the neighbors, not as the first).

30 Mar 2009 Andrea Tagliasacchi

kdtree_build() returns a pointer to the kdtree. That number needs to be fed as first argument to every other query method. I haven't tried it on older versions of Matlab but I haven't used any of the new releases functionality.

07 Feb 2009 Bing Jian

How to use the result returned by kdtree_build()
which is a double number on my MATLAB7.5.0?
Does your program require at least MATLAB7.6?
Thanks!

03 Feb 2009 Andrea Tagliasacchi

Thanks Kigor, I found a simple bug in the kdtree_nearest_neighbor data conversion. The query was executed MxN times instead of M times; given the query matrix Q is of size(M,N).

The updated kdtree_nearest_neighbor.cpp file can be found at
https://latis.cs.sfu.ca/svn/users/ata2/matlibs/kdtree/

The bug will be corrected in the next release

21 Jan 2009 KIgor Karic

For some reason kdtree_nearest_neighbor takes extremely long time to finish. Are others having the same problem or am I the only one?

03 Dec 2008 Luigi Giaccari

The most complete Kd-tree on Matlab exchange.
Thank you Andrea

30 Sep 2008 Gary Tam

Thank you! Work out of the box.

Updates
12 Nov 2008

Corrected few bugs in parameter passing
Greatly enhanced the preprocessing speed (tree construction) 10x
Improved examples and makefile structure.
More info in "CHANGES"

08 Dec 2008

Updates some interface bugs and the bugs on distances (second argument) returned by the query functions.

01 Sep 2010

Redirected documentation to my personal page

Contact us