【问题标题】:Matlab - finding nearest neighbor in three-dimensional coordinate system [duplicate]Matlab - 在三维坐标系中找到最近邻
【发布时间】:2015-02-24 12:23:42
【问题描述】:

欢迎!

我有一组 n matlab 矩阵,其结构如下:

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]

我尝试使用knnsearch不幸的是没有效果。感谢您的帮助!

【问题讨论】:

  • 请使用`代码段重新格式化问题。另外:您的意思是:我尝试使用knnsearch 没有效果?为什么这种方法不能令人满意?
  • 我找到了如何搜索二维坐标的描述。不幸的是,在三维系统的情况下,我不知道如何使用knnseach。此外,knneseach 的输入参数是单个矩阵(X 和 Y),而不是在我的情况下是一组多数组。
  • 查看this post 以获得洞察力。确保格式化数据,使每一行是一个点,每一列是一个变量。

标签: matlab matrix 3d nearest-neighbor


【解决方案1】:

是不是传统方式在某些方面不尽如人意?评估从每个点到测试点的距离,然后对距离进行排序...

%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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 2019-02-01
    • 1970-01-01
    • 2016-04-24
    • 2014-04-12
    • 2019-07-19
    • 2013-06-28
    • 2014-12-25
    相关资源
    最近更新 更多