1

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 ?

Peanut
  • 307
  • 1
  • 9

2 Answers2

5

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

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

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • Whenever I see your post, I try to pronounce your username as ubiquitin, as in the protein – Jason B. Mar 01 '16 at 10:20
  • 1
    @JasonB it is an ambigram: rotational symmetry (rotation by $\pi$)- my tiny homage to frugal typograph: only need u and b...:) – ubpdqn Mar 01 '16 at 10:26
2

Example:

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

Output:

output example

Credits:

@Kuba

Reference:

Outer

Tutorials:

List Manipulation

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
  • 1
    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 01 '16 at 10:14
  • @JasonB read your comment after posted my answer...oh well – ubpdqn Mar 01 '16 at 10:17
  • @JasonB please feel free to edit the post as you find necessary. Your insight is appreciated. – e.doroskevic Mar 01 '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 01 '16 at 10:53