# Euclidian distance matrix from a set of points [duplicate]

I have a set of points and I like to compute the (pairwise) euclidian distance for those points. The set has a form like this:

pts = {{1, 1}, {1, 2}, {3, 3}, {4, 5}}

I need the output as a matrix, because I like to use it for a weighted graph. I noticed that there is a function called DistanceMatrix[], however my Version doesn't support it. Is there a easy way using the EuclideanDistance[] function ?

• Given the size of my of my set speed is not an issue. Sorry I edit my question. Mar 1, 2016 at 10:07

There are a number of ways to get (symmetric) distance matrix, e.g

Outer[EuclideanDistance, pts, pts, 1]
Partition[EuclideanDistance @@@ Tuples[{pts, pts}], 4]


• Whenever I see your post, I try to pronounce your username as ubiquitin, as in the protein Mar 1, 2016 at 10:20
• @JasonB it is an ambigram: rotational symmetry (rotation by $\pi$)- my tiny homage to frugal typograph: only need u and b...:) Mar 1, 2016 at 10:26

Example:

pts = {{1, 1}, {1, 2}, {3, 3}, {4, 5}};
Outer[EuclideanDistance, pts, pts] // MatrixForm


Output:

Credits:

@Kuba

Reference:

Tutorials:

List Manipulation

• You should add a 1 as the fourth argument to Outer so that you have a matrix as the output rather than a Tensor. As it is you are taking the distance between points on the number line (I think) like EuclideanDistance[{1}, {3}] Mar 1, 2016 at 10:14