两个向量之间的欧氏距离(单行矩阵)

时间:2012-11-13 20:53:25

标签: matlab octave

我有两个向量(单行矩阵)。假设我们已经知道长度len

A = [ x1 x2 x3 x4 x5 .... ]
B = [ y1 y2 y3 y4 y5 .... ]

要计算它们之间的欧几里德距离,最快的方法是什么。我的第一次尝试是:

diff = A - B
sum = 0
for column = 1:len
    sum += diff(1, column)^2
distance = sqrt(sum)

我已经通过这种方法循环了数百万次。所以,我正在寻找快速和正确的东西。请注意,我没有使用MATLAB,也没有可用的pdist2 API。

3 个答案:

答案 0 :(得分:32)

diff = A - B;
distance = sqrt(diff * diff');

distance = norm(A - B);

答案 1 :(得分:0)

[val idx]    =  sort(sum(abs(Ti-Qi)./(1+Ti+Qi)));   

[val idx]    =  sort(sqrt(sum((Ti-Qi).^2))); 

Val是值,idx是应用欧几里德距离后要排序的列的原始索引值。 (Matlab代码)

答案 2 :(得分:0)

要添加到@kol回答,

diff = A - B;
distance = sqrt(sum(diff * diff')) % sum of squared diff

distance = norm(A-B);