All formulas

Linear algebra

Euclidean distance

linalg.euclidean-distanceAlso called euclidean distance, l2 distance, distance, dist

d(x,y)=∑i=1n(xi−yi)2d(x, y) = \sqrt{\sum_{i=1}^{n} (x_i - y_i)^2}

Straight-line distance between two points: the L2 norm of their difference.

Notation

xx

first vector

x ∈ ℝn

yy

second vector

y ∈ ℝn

nn

vector length

Size, at least 1

d(x,y)d(x, y)

Result: the distance

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=[0,0],y=[3,4]x = [0, 0], y = [3, 4]

    Result

    d(x,y)=5\displaystyle d(x, y) = 5
  2. Given

    x=[1,2,3],y=[1,2,3]x = [1, 2, 3], y = [1, 2, 3]

    Result

    d(x,y)=0\displaystyle d(x, y) = 0
  3. Given

    x=[1,0,−1],y=[0,1,1]x = [1, 0, -1], y = [0, 1, 1]

    Result

    d(x,y)=6\displaystyle d(x, y) = \sqrt{6}

    ≈ 2.44948974278

Common mistakes

  • Returns the squared distance

    The final square root is missing.

    Fix: Take the square root of the sum of squared differences.

Source

  • Golub and Van Loan 2013

    Golub, Gene H.; Van Loan, Charles F. Matrix Computations. 4th ed. Johns Hopkins University Press, 2013.