3
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Look up DistanceMatrix[]. "Without duplicates" means you'll only need the strict upper triangle of that matrix. $\endgroup$ – J. M. will be back soon Jun 25 '16 at 17:28
  • $\begingroup$ What about yourCondition/@yourDistanceFunction@@@Permutations[list]? $\endgroup$ – JungHwan Min Jun 25 '16 at 17:31
  • $\begingroup$ Following @J.M. 's solution, UpperTriangularize[] would be appropriate to use with DistanceMatrix[]. $\endgroup$ – JungHwan Min 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
4
$\begingroup$

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}]

$\left\{1,\sqrt{85},\sqrt{113},\sqrt{74},10,\sqrt{2}\right\}$

Here below only a presentation of the steps

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

$1$

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

$\sqrt{85}$

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

$\sqrt{113}$

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

$\sqrt{74}$

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

$10$

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

$\sqrt{2}$

$\endgroup$

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.