Euclidean distance between two n-dimensional vectors
Asked Answered
Z

3

15

What's an easy way to find the Euclidean distance between two n-dimensional vectors in Julia?

Zina answered 18/9, 2014 at 0:14 Comment(0)
T
20

Here is a simple way

n = 10
x = rand(n)
y = rand(n)
d = norm(x-y)  # The euclidean (L2) distance

For Manhattan/taxicab/L1 distance, use norm(x-y,1)

Timbuktu answered 18/9, 2014 at 1:39 Comment(1)
In Julia 1.0 you have to call using LinearAlgebra first.Octangular
Z
14

This is easily done thanks to the lovely Distances package:

Pkg.add("Distances") #if you don't have it
using Distances
one7d = rand(7)
two7d = rand(7)
dist = euclidean(one7d,two7d)

Also if you have say 2 matrices of 9d col vectors, you can get the distances between each corresponding pair using colwise:

thousand9d1 = rand(9,1000)
thousand9d2 = rand(9,1000)
dists = colwise(Euclidean(), thousand9d1, thousand9d2)
#returns: 1000-element Array{Float64,1}

You can also compare to a single vector e.g. the origin (if you want the magnitude of each column vector)

origin9 = zeros(9)
mags = colwise(Euclidean(), thousand9ds1, origin9)
#returns: 1000-element Array{Float64,1}

Other distances are also available:

  • Squared Euclidean
  • Cityblock
  • Chebyshev
  • Minkowski
  • Hamming
  • Cosine
  • Correlation
  • Chi-square
  • Kullback-Leibler divergence
  • Jensen-Shannon divergence
  • Mahalanobis
  • Squared Mahalanobis
  • Bhattacharyya
  • Hellinger

More details at the package's github page here.

Zina answered 18/9, 2014 at 0:14 Comment(2)
+1 Thanks for bringing up the Distances package. I think OP's question is sufficiently answered by norm(), but it is useful to know of the package.Heyward
This also works for dimensions above 2 (norm only supports 1- or 2-dimensional arrays). E.g., norm(rand((4,4,4)) - rand((4,4,4))) will failForegather
S
0

To compute the norm without any imports, simply

norm(x) = sqrt(sum(x.^2))

More generic for other L1, L2, L3, ... norms

norm(x; L=2) = sum(abs.(x).^L) ^ (1/L)

For Euclidean distance between two n-dimensional vectors just call norm(x-y).

Stillness answered 16/7, 2023 at 7:10 Comment(1)
sqrt(sum(x.^2)) creates an unnecessary intermediate array. Better to write sqrt(sum(abs2, x))Floccus

© 2022 - 2024 — McMap. All rights reserved.