# 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 ?

## marked as duplicate by dr.blochwave, Kuba♦, Artes, MarcoB, m_goldbergMar 1 '16 at 13:02

• Given the size of my of my set speed is not an issue. Sorry I edit my question. – Peanut Mar 1 '16 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 – Jason B. Mar 1 '16 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 '16 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}] – Jason B. Mar 1 '16 at 10:14
• @JasonB read your comment after posted my answer...oh well – ubpdqn Mar 1 '16 at 10:17
• @JasonB please feel free to edit the post as you find necessary. Your insight is appreciated. – e.doroskevic Mar 1 '16 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? – Jason B. Mar 1 '16 at 10:53