Julia 两个n维向量之间的欧氏距离
在Julia中,找到两个n维向量之间的欧几里德距离的简单方法是什么?由于可爱的软件包,这很容易做到: 另外,如果您有两个9d列向量矩阵,您可以使用colwise获得每个对应对之间的距离:Julia 两个n维向量之间的欧氏距离,julia,euclidean-distance,Julia,Euclidean Distance,在Julia中,找到两个n维向量之间的欧几里德距离的简单方法是什么?由于可爱的软件包,这很容易做到: 另外,如果您有两个9d列向量矩阵,您可以使用colwise获得每个对应对之间的距离: thousand9d1 = rand(9,1000) thousand9d2 = rand(9,1000) dists = colwise(Euclidean(), thousand9d1, thousand9d2) #returns: 1000-element Array{Float64,1} 还可以与单个
thousand9d1 = rand(9,1000)
thousand9d2 = rand(9,1000)
dists = colwise(Euclidean(), thousand9d1, thousand9d2)
#returns: 1000-element Array{Float64,1}
还可以与单个向量进行比较,例如原点(如果需要每个列向量的大小)
其他距离也可用:
- 平方欧几里得
- 城市街区
- 切比雪夫
- 明可夫斯基
- 哈明
- 余弦
- 相关性
- 卡方
- Kullback-Leibler散度
- 詹森-香农散度
- 马哈拉诺比斯
- 方形马氏体
- 巴塔查里亚
- 海林格
n = 10
x = rand(n)
y = rand(n)
d = norm(x-y) # The euclidean (L2) distance
对于曼哈顿/出租车/L1距离,请使用
norm(x-y,1)
+1感谢您提出距离
套餐。我认为OP的问题已经被norm()
充分回答了,但是了解这个包很有用。这也适用于2维以上的维度(norm只支持1维或2维数组)。例如,norm(rand((4,4,4))-rand((4,4,4))
将在Julia 1.0中失败,您必须首先使用LinearGebra调用。
origin9 = zeros(9)
mags = colwise(Euclidean(), thousand9ds1, origin9)
#returns: 1000-element Array{Float64,1}
n = 10
x = rand(n)
y = rand(n)
d = norm(x-y) # The euclidean (L2) distance