What's an easy way to find the Euclidean distance between two n-dimensional vectors in Julia?
Asked
Active
Viewed 1.1k times
3 Answers
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)

IainDunning
- 11,546
- 28
- 43
-
4In Julia 1.0 you have to call `using LinearAlgebra` first. – Amade Sep 26 '18 at 12:18
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.

JobJob
- 3,822
- 3
- 33
- 34
-
3+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. – Zhubarb May 01 '15 at 10:00
-
1This 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 fail – Bill Gross Jun 16 '16 at 01:17
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)
.

Friedrich -- Слава Україні
- 2,901
- 1
- 21
- 40
-
1`sqrt(sum(x.^2))` creates an unnecessary intermediate array. Better to write `sqrt(sum(abs2, x))` – DNF Jul 16 '23 at 10:51