1
$\begingroup$

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 ?

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

2 Answers 2

5
$\begingroup$

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

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

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

$\endgroup$
4
  • 1
    $\begingroup$ 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}] $\endgroup$
    – Jason B.
    Mar 1, 2016 at 10:14
  • $\begingroup$ @JasonB read your comment after posted my answer...oh well $\endgroup$
    – ubpdqn
    Mar 1, 2016 at 10:17
  • $\begingroup$ @JasonB please feel free to edit the post as you find necessary. Your insight is appreciated. $\endgroup$ Mar 1, 2016 at 10:18
  • $\begingroup$ @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? $\endgroup$
    – Jason B.
    Mar 1, 2016 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.