【问题标题】:How to write code the Chebyshev and Mahalanobis distance using bsxfun function in matlab?如何在matlab中使用bsxfun函数编写切比雪夫和马氏距离的代码?
【发布时间】:2018-08-16 03:02:02
【问题描述】:

我找到了使用欧几里得距离计算两个矩阵距离的示例代码:Finding K-nearest neighbors and its implementation 数据矩阵如下:

load fisheriris 
X = meas(:,3:4);
newpoints = [5 1.45; 7 2; 4 2.5; 2 3.5];

我将如何应用切比雪夫和马氏距离并替换以下函数:

%// Use Euclidean
dists = sqrt(sum(bsxfun(@minus, x, newpoint).^2, 2));

我尝试将代码更改为:

dists = max(abs(bsxfun(@minus, X, newpoint)))

答案如下。可能是因为我根据公式放了max函数。

dists2 =
      4.0000    1.3500

但是,如果我使用这个 knnsearch 代码,它会按预期工作。但是我需要应用 bsxfun 以便我的代码将使用上层代码进行标准化。我想比较我算法中的不同距离:

[ncb,dcb] = knnsearch(X,newpoint,'k',10,'distance','chebychev')

如果有人可以帮助我,不胜感激。

【问题讨论】:

    标签: matlab distance bsxfun mahalanobis


    【解决方案1】:

    你可以在这里找到我的答案的扩展版本:

    https://de.mathworks.com/matlabcentral/answers/386963-how-to-write-code-the-chebyshev-and-mahalanobis-distance-using-bsxfun-function

    精髓:

    load fisheriris 
    oldpoints = meas(:,3:4);
    newpoints = [5 1.45; 7 2; 4 2.5; 2 3.5];
    newpoints = permute(newpoints, [3,2,1]);
    % Euclidean distance
    dists_euclid = sqrt(sum(bsxfun(@minus, newpoints, oldpoints).^2, 2));
    % Chebyshev distance
    dists_cheby = max(abs(bsxfun(@minus, oldpoints, newpoints)),[],2);
    

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 2016-04-21
      • 2013-11-24
      • 2018-06-05
      • 2017-09-15
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多