I am new to Mathematica so the answer to this question is probably easy.

I have a list of 2D-points in the form {{x1, y1}, {x2, y2}, ...} Then I have a function that will take two points and test some condition based on their distance. I want to test all points against each other, without duplicates.

How can you do that?

  • 1
    $\begingroup$ Look up DistanceMatrix[]. "Without duplicates" means you'll only need the strict upper triangle of that matrix. $\endgroup$
    – J. M.'s torpor
    Jun 25 '16 at 17:28
  • $\begingroup$ What about yourCondition/@yourDistanceFunction@@@Permutations[list]? $\endgroup$ Jun 25 '16 at 17:31
  • $\begingroup$ Following @J.M. 's solution, UpperTriangularize[] would be appropriate to use with DistanceMatrix[]. $\endgroup$ Jun 25 '16 at 17:34
  • 6
    $\begingroup$ EuclideanDistance @@@ Subsets[yourList, {2}] $\endgroup$
    – yode
    Jun 25 '16 at 17:34
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jun 25 '16 at 17:39

Based on comment from yode

Let us suppose a list:

list = {{1, 1}, {2, 1}, {7, 8}, {8, 9}}

$\left( \begin{array}{cc} 1 & 1 \\ 2 & 1 \\ 7 & 8 \\ 8 & 9 \\ \end{array} \right)$

With this function you can calculate the distances of each point without repetitions:

EuclideanDistance @@@ Subsets[list, {2}]


Here below only a presentation of the steps

EuclideanDistance[list[[2]], list[[1]]]


EuclideanDistance[list[[3]], list[[1]]]


EuclideanDistance[list[[4]], list[[1]]]


EuclideanDistance[list[[2]], list[[3]]]


EuclideanDistance[list[[2]], list[[4]]]


EuclideanDistance[list[[3]], list[[4]]]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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