Asked by bala k
on 9 Mar 2011

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.

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

Jared Willard
on 2 Oct 2017

I don't know about others but I am getting entirely different results when comparing

D = sqrt(sum((G - G2) .^ 2));

and

D = norm(G - G2);

James Tursa
on 2 Oct 2017

What are your G and G2 sizes? Are they real or complex?

Jan
on 2 Oct 2017

@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.

Opportunities for recent engineering grads.

Apply Today
## 1 Comments

## juit (view profile)

Direct link to this comment:https://ww2.mathworks.cn/matlabcentral/answers/2849-euclidean-distance-of-two-vectors#comment_415890

Sign in to comment.