Euclidian distance matrix from a set of points [duplicate]

This question already has an answer here:

Fastest way to calculate matrix of pairwise distances

4 answers

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 ?



2 Answers


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
– JasonB
Mar 1 at 10:20



@JasonB it is an ambigram: rotational symmetry (rotation by π\pi)- my tiny homage to frugal typograph: only need u and b…:)
– ubpdqn
Mar 1 at 10:26


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







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}]
– JasonB
Mar 1 at 10:14



@JasonB read your comment after posted my answer…oh well
– ubpdqn
Mar 1 at 10:17



@JasonB please feel free to edit the post as you find necessary. Your insight is appreciated.
– e.doroskevic
Mar 1 at 10:18



@Artes, you deleted your comment so maybe you won’t see this. Sorry if my reply came off as harsh, would you care to discuss it in the chat room?
– JasonB
Mar 1 at 10:53