# Computing Euclidean distance between 2 points

2 visualizzazioni (ultimi 30 giorni)
Elysi Cochin il 3 Mag 2022
Modificato: Bruno Luong il 3 Mag 2022
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?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Rik il 3 Mag 2022
Modificato: Rik il 3 Mag 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.
q=rand(1,5);p=rand(1,5);
diff=q-p;
diff * diff' , sum(diff .* diff)
ans = 0.6624
ans = 0.6624
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Bruno Luong il 3 Mag 2022
Modificato: Bruno Luong il 3 Mag 2022
For a row vector d
% sum(d.^2)
is equal to
% d*d'
Example
d=randi(10,1,3)
d = 1×3
9 6 5
sum(d.^2)
ans = 142
d*d'
ans = 142
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Smoothing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by