How to find all neighbours of an element in N-dimensional matrix
Show older comments
I just can't wrap my head around how to find all neighbours of an element in a N-dimensional matrix. After reading through the documentation on spatial searching I think it could be done using the Delaunay triangulation. However, these topics are still a bit too advanced for me, so any help would be appreciated.
1 Comment
Accepted Answer
More Answers (3)
Shashank Prasanna
on 11 Sep 2013
If you have the Statistics Toolbox installed, there are couple of nearest neighbor searching tools that you might find very useful. Although some of these concepts may be advanced for a casual user, the functionality itself is very quite easy to use, that's really the power of MATLAB:
One way to speed up higher dimensional neighbor searching is to use an optimized data structure such as k dimensional trees, and this is easy to do so as below:
kdtreeNS = KDTreeSearcher(x);
[n,d]=knnsearch(kdtreeNS,x);
Here is the page from the doc:
2 Comments
dave
on 11 Sep 2013
Shashank Prasanna
on 11 Sep 2013
Michael, I misunderstood your question. I made the assumption that you were referring to the columns as dimensions in |R^n space .
None of these function work on multidimensional matrices. If you have data in some n dimensional space each instance or observation can be represented as a vector with n entries. I am not sure how your data is represented but if you are able to represent it in a why described you can use all the functionality that I mentioned to do your neighbor search.
Image Analyst
on 11 Sep 2013
1 vote
You just take the index, the index plus one, and the index minus one, for every other dimension, but exclude the index of where you're at. For example if you have a 3D matrix, and you're at x=3, y=6, and z=9, you'd have all permutations of x in [2,3,4] with y in [5,6,7] with z in [8,9,10] but don't include the point itself with x=3,y=6, z=9. So that's 3*3*3-1 neighbors in 3D. For N dimensions you'd have 3 * 3 * 3 * ....(a total N times) * 3 -1 neighbors. So 2D gives 3*3-1 = 8 neighbors, 3D gives 26 neighbors, 4D gives 80 neighbors, etc.
6 Comments
Image Analyst
on 11 Sep 2013
By the way, you mgiht like this video. http://www.youtube.com/watch?v=JkxieS-6WuA. It does a decent job of explaining all the dimensions up to the tenth dimension.
dave
on 11 Sep 2013
Image Analyst
on 11 Sep 2013
Yes it does. I believe though that you can carve out the box using indexing, for example:
subHyperVolume = hyperVolume(4:6, 18:20, 9:11, 7:9, 1:3);
though this sub volume would have the "center" element.
Jan
on 11 Sep 2013
When I speak of a larger number of dimensions, I mean thousands to millions. Then interesting effects occur, e.g. that almost all points have almost the same distance. And the ratio between the volume of a hyper-cube and the embedded (or is it called "included") hyper-sphere has a surprising relation to the number of dimensions also.
But such large spaces are very hard to implement in Matlab, because the number of indexed elements explodes.
Image Analyst
on 11 Sep 2013
I'd use loops over each dimension, so for 5 dimensions like my example
for d1=1:d1max
for d2=1:d2max
for d3=1:d3max
for d4=1:d4max
for d5=1:d5max
subHyperVolume = hyperVolume(...
d1-1:d1+1,...
d2-1:d2+1,...
d3-1:d3+1,...
d4-1:d4+1,...
d5-1:d5+1,...
);
end
end
end
end
end
Sean de Wolski
on 24 Jun 2014
0 votes
No need to reinvent the wheel:
Categories
Find more on Image Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!