美文网首页MATLAB
pdist、squareform

pdist、squareform

作者: 庵下桃花仙 | 来源:发表于2018-09-29 10:44 被阅读13次

    pdist

    成对之间距离
    D = pdist(X)
    返回观察对之间的欧几里德距离 X。

    例子

    计算欧式距离并将距离矢量转换为矩阵

    计算观察对之间的欧几里德距离,并使用将距离向量转换为矩阵squareform。

    创建一个包含三个观察值和两个变量的矩阵。

    rng('default')
     X = rand(3, 2);
     D = pdist(X)
    
    D =
    
        0.2954    1.0670    0.9448
    

    成对距离按(2,1),(3,1),(3,2)的顺序排列。您可以轻松找到观察之间的距离i并j使用squareform。

    Z = squareform(D)

    Z =
    
             0    0.2954    1.0670
        0.2954         0    0.9448
        1.0670    0.9448         0
    

    squareform返回一个对称矩阵,其中Z(i,j)对应于观察i与之间的成对距离j。例如,您可以找到观察2和3之间的距离。

    传递Z给squareform函数重现函数的输出pdist。

    y = squareform(Z)

    y =
    
        0.2954    1.0670    0.9448
    

    相关文章

      网友评论

        本文标题:pdist、squareform

        本文链接:https://www.haomeiwen.com/subject/tesvoftx.html