【发布时间】:2014-11-12 04:21:57
【问题描述】:
在 Julia 中找到两个 n 维向量之间的欧几里得距离的简单方法是什么?
【问题讨论】:
在 Julia 中找到两个 n 维向量之间的欧几里得距离的简单方法是什么?
【问题讨论】:
感谢可爱的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}
其他距离也可以:
更多详情请关注package's github page here.
【讨论】:
Distances 软件包。我认为norm() 已充分回答了 OP 的问题,但了解该软件包很有用。
norm(rand((4,4,4)) - rand((4,4,4))) 将失败
这是一个简单的方法
n = 10
x = rand(n)
y = rand(n)
d = norm(x-y) # The euclidean (L2) distance
对于曼哈顿/出租车/L1 距离,使用norm(x-y,1)
【讨论】:
using LinearAlgebra。