MATLAB Answers

bala k

Euclidean distance of two vectors

Asked by bala k
on 9 Mar 2011
Latest activity Edited by MathWorks Support Team on 8 Nov 2018
Accepted Answer by Jan
Euclidean distance of two vector. I have the two image values G=[1x72] and G1 = [1x72]. I need to calculate the two image distance value.


i have three points a(x1,y1) b(x2,y2) c(x3,y3) i have calculated euclidean distance d1 between a and b and euclidean distance d2 between b and c. if now i just want to travel through a path like from a to b and then b to c. can i add d1 and d2 to calculate total distance traveled by me???

Sign in to comment.

1 Answers

Answer by Jan
on 9 Mar 2011
Edited by Jan
on 16 Apr 2016
 Accepted Answer

Do you know the definition of the Euclidean distance?
G = rand(1, 72);
G2 = rand(1, 72);
D = sqrt(sum((G - G2) .^ 2));
A more efficient method, but this matters only for much larger vectors:
V = G - G2;
D = sqrt(V * V');
Or a Matlab command:
D = norm(G - G2);
D = DNorm2(G - G2);
Search in the documentation before asking in MATLAB Answers:
docsearch euclidean


I don't know about others but I am getting entirely different results when comparing
D = sqrt(sum((G - G2) .^ 2));
D = norm(G - G2);
What are your G and G2 sizes? Are they real or complex?
@Jared: Please mention any details. What is the size and contents of your G and G2 variables? Check if your have redefined sqrt, sum or norm. Try:
format long g
clear variables
G = rand(1, 72);
G2 = rand(1, 72);
D1 = sqrt(sum((G - G2) .^ 2))
V = G - G2;
D2 = sqrt(V * V')
D3 = norm(G - G2)
Looks fine. And of course: The formulas are identical from a mathematical point of view. Then I'm sure you have overseen a detail in your comparison.

Sign in to comment.