Code covered by the BSD License

### Highlights fromkd-tree for matlab

4.81818

4.8 | 11 ratings Rate this file 120 Downloads (last 30 days) File Size: 129 KB File ID: #21512

# kd-tree for matlab

22 Sep 2008 (Updated 01 Sep 2010)

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

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

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
Everyone's Tags
Tags I've Applied
22 Mar 2013

Thank you! Gave my application 100x speedup.

07 Aug 2012

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?

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

15 Jun 2012

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

14 Jun 2012

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)

04 Jun 2012

@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

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

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

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

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

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

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

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

10 Feb 2010

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

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

Ok, Shame on me, there is a method

"kdtree_delete"

that needs to be called for releasing the memory again.

22 Oct 2009

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

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

09 Sep 2009

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

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

17 May 2009

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

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

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

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

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

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

30 Sep 2008

Thank you! Work out of the box.

24 Sep 2008

The makefile was implying an underlying .mexmaci initialization which is valid only for mac/OSX environments. The mexext program is now used to determine the extension of the build products according to your architecture.

12 Nov 2008

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