Matlab-在三维坐标系中寻找最近邻
欢迎光临 我有一组n个matlab矩阵,其结构如下:Matlab-在三维坐标系中寻找最近邻,matlab,matrix,3d,nearest-neighbor,Matlab,Matrix,3d,Nearest Neighbor,欢迎光临 我有一组n个matlab矩阵,其结构如下: xyz_1 -3,37200000000000 2,80000000000000 5,03400000000000 xyz_2 -2,21700000000000 1,74500000000000 7,45300000000000 .... .................. ................ ................ xyz_n -1,39300000000000
xyz_1 -3,37200000000000 2,80000000000000 5,03400000000000
xyz_2 -2,21700000000000 1,74500000000000 7,45300000000000
.... .................. ................ ................
xyz_n -1,39300000000000 0,00700000000000000 6,35500000000000
其中第一列是矩阵的名称,而接下来的三列是xyz坐标。我正在寻找一种找到最近邻居的有效方法。我想给出矩阵名和潜在邻居的k作为输入参数,然后程序将查找最近邻居,并以以下形式给出结果矩阵:
[nearest_neighbor_name_1; distance_between_quoted_element_and_nearest_neigbor_1
nearest_neighbor_name_2; distance_between_quoted_element_and_nearest_neigbor_2
nearest_neighbor_name_....; distance_between_quoted_element_and_nearest_neigbor_....
nearest_neighbor_name_k; distance_between_quoted_element_and_nearest_neigbor_k]
不幸的是,我试图使用KNN,但没有效果。谢谢你的帮助 在某种程度上,传统的工作方式不令人满意吗?评估从每个点到测试点的距离,然后对距离进行排序
%define the "k" entries that you are interested in assessing
mydata_xyz = [-3.37200000000000 2.80000000000000 5.03400000000000;
-2.21700000000000 1.74500000000000 7.45300000000000;
<the rest of your data here>
-1.39300000000000 0.00700000000000000 6.35500000000000];
%define the point about which you are looking for the nearest neighbors
mypoint_xyz = [ <whatever your xyz coordinate is];
%compute the distance from the given point to all of the other test points.
%Note the use of the transpose (apostrophe) to ensure that it sums in the
% correct direction
distance = sqrt(sum( ((mydata_xyz - ones(size(mydata_xyz,1),1)*mypoint_xyz).^2)' ));
%sort to get the nearest neighbor followed by the next nearest neighbors
[sorted_distance, Isort] = sort(distance);
%print out each one of the points, from closest to farthest
for I=1:length(Isort)
disp(['Point ' num2str(Isort) ...
', dist = ' num2str(distance(Isort(I))) ...
', xyz = ' num2str(mydata_xyz(Isort(I),:))]);
end
请使用“代码段”重新格式化问题。还有:你所说的:我试图使用knnsearch而没有效果是什么意思?为什么这种方法不令人满意?我找到了如何搜索二维坐标的描述。不幸的是,我不知道如何在三维系统中使用knnseach。此外,knneseach的输入参数是单个矩阵X和Y,而不是我的例子中的一组多数组。确保格式化数据,使每一行都是一个点,每一列都是一个变量。