Ridiculously Simple Nearest Neighbor Search 3D
15 次查看(过去 30 天)
显示 更早的评论
Hello,
What is the best way to implement a nearest neighbor search between 3d points, here I have 2 sets of 3d points where the matrices are not the same size. The goal is compute the nearest neighbor to the 1st point in the first set with all the points in the second set and then index it. On top of that then I have to do a NNS search between one point and points in other set as well as the other points in the same set.
0 个评论
回答(2 个)
Kelly Kearney
2013-8-8
It's sort of overkill, but I usually use interpolation to do this ( scatteredInterpolant in the latest version of Matlab, previously used TriScatteredInterp or griddata).
Here's an example in 2D, but it works exactly the same in 3D:
% Sample data
x1 = rand(10,1);
y1 = rand(10,1);
x2 = rand(5,1);
y2 = rand(5,1);
% Calculate index of set 1 closest to each point in set 2
F = scatteredInterpolant(x1, y1, (1:10)', 'nearest', 'nearest')
idx = F(x2,y2)
% A plot to check
scatter(x1,y1,[], (1:10)', 'filled');
hold on;
scatter(x2,y2,[], idx);
axis equal
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!