Computing Euclidean distance between 2 points

3 views (last 30 days)
I found a code computing Euclidean distance as
diff = q - p;
dist = sqrt(diff * diff');
is there any difference from the standard Euclidean distance formula, why transpose is taken?

Accepted Answer

Rik on 3 May 2022
Edited: Rik on 3 May 2022
By doing a matrix multiplication on a vector, you implicitly take the sum over all elements.
Note that this will only work if both p and q are row vectors.
diff * diff' , sum(diff .* diff)
ans = 0.6624
ans = 0.6624

More Answers (1)

Bruno Luong
Bruno Luong on 3 May 2022
Edited: Bruno Luong on 3 May 2022
For a row vector d
% sum(d.^2)
is equal to
% d*d'
d = 1×3
9 6 5
ans = 142
ans = 142


Find more on Physics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by