【问题标题】:Euclidean distance between two n-dimenstional vectors两个 n 维向量之间的欧几里得距离
【发布时间】:2014-11-12 04:21:57
【问题描述】:

在 Julia 中找到两个 n 维向量之间的欧几里得距离的简单方法是什么?

【问题讨论】:

    标签: julia euclidean-distance


    【解决方案1】:

    感谢可爱的Distances 包,这很容易做到:

    Pkg.add("Distances") #if you don't have it
    using Distances
    one7d = rand(7)
    two7d = rand(7)
    dist = euclidean(one7d,two7d)
    

    此外,如果您有 2 个 9d col 向量矩阵,您可以使用 colwise 获得每个对应对之间的距离:

    thousand9d1 = rand(9,1000)
    thousand9d2 = rand(9,1000)
    dists = colwise(Euclidean(), thousand9d1, thousand9d2)
    #returns: 1000-element Array{Float64,1}
    

    您还可以与单个向量进行比较,例如原点(如果你想要每个列向量的大小)

    origin9 = zeros(9)
    mags = colwise(Euclidean(), thousand9ds1, origin9)
    #returns: 1000-element Array{Float64,1}
    

    其他距离也可以:

    • 欧几里得平方
    • 城市街区
    • 切比雪夫
    • 闵可夫斯基
    • 汉明
    • 余弦
    • 相关性
    • 卡方
    • Kullback-Leibler 散度
    • Jensen-Shannon 分歧
    • 马氏菌
    • 马氏平方
    • 巴塔查亚
    • 海灵格

    更多详情请关注package's github page here.

    【讨论】:

    • +1 感谢您提出Distances 软件包。我认为norm() 已充分回答了 OP 的问题,但了解该软件包很有用。
    • 这也适用于 2 以上的维度(norm 仅支持 1 维或 2 维数组)。例如,norm(rand((4,4,4)) - rand((4,4,4))) 将失败
    【解决方案2】:

    这是一个简单的方法

    n = 10
    x = rand(n)
    y = rand(n)
    d = norm(x-y)  # The euclidean (L2) distance
    

    对于曼哈顿/出租车/L1 距离,使用norm(x-y,1)

    【讨论】:

    • 在 Julia 1.0 中,您必须先调用 using LinearAlgebra
    猜你喜欢
    • 2014-05-31
    • 2014-06-14
    • 2014-03-27
    • 1970-01-01
    • 2016-04-09
    • 2019-06-19
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    相关资源
    最近更新 更多