【问题标题】:Matlab calculating nearest neighbour distance for all (u, v) vectors in an arrayMatlab计算数组中所有(u,v)向量的最近邻距离
【发布时间】:2015-03-21 20:25:53
【问题描述】:

我正在尝试计算 nx2 矩阵中最近邻居之间的距离,如下所示

point_coordinates =

   11.4179  103.1400
   16.7710   10.6691
   16.6068  119.7024
   25.1379   74.3382
   30.3651   23.2635
   31.7231  105.9109
   31.8653   36.9388





%for loop going from the top of the vector column to the bottom
for counter = 1:size(point_coordinates,1) 
    %current point defined selected 
    current_point = point_coordinates(counter,:);

    %math to calculate distance between the current point and all the points 
    distance_search= point_coordinates-repmat(current_point,[size(point_coordinates,1) 1]);
    dist_from_current_point = sqrt(distance_search(:,1).^2+distance_search(:,2).^2);

    %line to omit self subtraction that gives zero
    dist_from_current_point (dist_from_current_point <= 0)=[];

    %gives the shortest distance calculated for a certain vector and current_point
    nearest_dist=min(dist_from_current_point);

end

%final line to plot the u,v vectors and the corresponding nearest neighbour
%distances
matnndist = [point_coordinates nearest_dist]

我不确定如何构造“for”循环/nearest_neighbour 线以获取每个 u,v 向量的最近邻距离。

我想拥有,例如; 对于第一个向量,你可以有坐标和相应的最短距离,对于第二个向量,你可以有另一个最短距离,这一直持续到 n

希望有人能提供帮助。

谢谢

【问题讨论】:

    标签: matlab for-loop matrix vector distance


    【解决方案1】:

    我了解您想获得不同点之间的最小距离

    您可以使用bsxfun 计算每对点的距离;消除自我距离;最小化。使用平方距离的计算效率更高,并且只在最后取平方根。

    n = size(point_coordinates,1);
    dist = bsxfun(@minus, point_coordinates(:,1), point_coordinates(:,1).').^2 + ...
           bsxfun(@minus, point_coordinates(:,2), point_coordinates(:,2).').^2;
    dist(1:n+1:end) = inf; %// remove self-distances
    min_dist = sqrt(min(dist(:)));
    

    或者,您可以使用pdist。这避免了计算每个距离两次,也避免了自距离:

    dist = pdist(point_coordinates);
    min_dist = min(dist(:));
    

    【讨论】:

      【解决方案2】:

      如果我可以建议一个内置函数,请使用统计工具箱中的knnsearch。您实际上在做的是K-Nearest Neighbour (KNN) 算法,但您忽略了自距离。您调用knnsearch 的方式如下:

      [idx,d] = knnsearch(X, Y, 'k', k);
      

      简单来说,KNN 算法返回k 最接近 点到您的数据集给定查询点。通常,欧几里得距离是使用的距离度量。对于 MATLAB 的 knnsearchX 是一个二维数组,由您的数据集组成,其中每个 是一个观察值,每个 是一个变量。 Y 将是查询点。 Y 也是一个二维数组,其中每个 是一个查询点,您需要与 X 具有相同数量的列。我们还将指定标志'k' 来表示您想要返回多少最近的点。默认为k = 1

      因此,idx 将是一个 N x K 矩阵,其中 N 是查询点的总数(Y 的行数),K 将是那些最接近的点 k我们拥有的每个查询点的数据集。 idx 表示数据集中最接近每个查询的特定点。 d 也是一个 N x K 矩阵,它返回这些对应最近点的最小距离

      因此,您要做的是为您的数据集找到与其他每个点最近的点,忽略自身距离。因此,您可以将XY 设置为相同,并设置k = 2,丢弃两个输出的第一列以获得您要查找的结果。

      因此:

      [idx,d] = knnsearch(point_coordinates, point_coordinates, 'k', 2)
      idx = idx(:,2);
      d = d(:,2);
      

      因此我们得到idxd

      >> idx
      
      idx =
      
           3
           5
           1
           1
           7
           3
           5
      
      >> d
      
      d =
      
         17.3562
         18.5316
         17.3562
         31.9027
         13.7573
         20.4624
         13.7573
      

      因此,这告诉我们,对于数据集中的第一个点,它与第 3 点匹配得最好。这与最近的距离 17.3562 相匹配。对于数据集中的第二个点,它与点 #5 匹配得最好,最近的距离是 18.5316。您可以以类似的模式继续处理其余结果。


      如果您无法访问统计工具箱,请考虑阅读我的 StackOverflow 帖子,了解我如何根据第一原理计算 KNN。

      Finding K-nearest neighbors and its implementation

      其实和之前给你的Luis Mendo's post很像。


      祝你好运!

      【讨论】:

        猜你喜欢
        • 2019-05-26
        • 1970-01-01
        • 2011-03-10
        • 2012-03-18
        • 2018-12-02
        • 2018-12-20
        • 2019-02-21
        • 1970-01-01
        相关资源
        最近更新 更多