1
$\begingroup$

I would like to find the distance between a point list (i.e dalist) and list of centers (i.e framecenters). I used the following script but it did not work. I do not want to use a double for loop. Any help is appreciated.

dalist = {{9, 6}, {5, 6}, {6, 0}, {0, 5}, {10, 8}, {1, 2}, {10, 
4}, {1, 1}, {7, 7}, {6, 8}, {5, 3}, {6, 9}, {7, 4}, {1, 8}, {10, 
0}, {10, 7}, {6, 3}, {4, 0}, {9, 2}, {4, 7}, {1, 6}, {10, 8}, {7, 
8}, {0, 9}, {3, 4}, {0, 0}, {8, 5}, {4, 5}, {6, 0}, {2, 9}, {2, 
4}, {8, 4}, {7, 4}, {3, 6}, {7, 9}, {1, 9}, {1, 4}, {8, 0}, {8, 
9}, {5, 4}, {2, 5}, {2, 9}, {3, 1}, {0, 6}, {10, 3}, {9, 6}, {8, 
7}, {7, 6}, {7, 3}, {8, 9}, {7.5, 9}, {6.5, 9}, {7, 9}, {1, 
5}, {2, 6}, {1, 10}, {0.5, 8}, {1.5, 8}, {0.5, 7}, {1.5, 7}, {0.5,
 6}, {1.5, 6}, {0.5, 5}, {1.5, 5}, {0.5, 4}, {1.5, 4}, {0.5, 
9}, {1.5, 9}, {1, 7}, {2, 8}, {7, 10}, {9, 4}, {8, 4}, {8, 3}, {9,
 5}, {9, 3}, {7.5, 3}, {8.5, 3}, {9.5, 4}, {8.5, 4}, {9.5, 
4}, {7.5, 4}, {9.5, 4}};

frameCenters = {{1, 10}, {7, 10}, {9, 4}};
filter1[list_, peanut_] := EuclideanDistance[#, peanut] &
filter1[dalist, #] & /@ frameCenters  ``` 
$\endgroup$
  • 2
    $\begingroup$ If dalist and frameCenters are both lists of {x,y} coordinates, you might use Outer[EuclideanDistance, dalist, frameCenters]. See Outer $\endgroup$ – Jason B. Jun 8 at 15:42
  • $\begingroup$ Thank you! But why it just shows a list of the list. {{{{8, 1}, {2, 1}, {0, 5}}, {{5, 4}, {1, 4}, {3, 2}}}....}. Also, my objective was to learn how to use functions and syntaxes so it would be great if you can modify the function. $\endgroup$ – Mehdi Ebadi Jun 8 at 16:13
3
$\begingroup$

If I understand it correctly, you're looking to compute the distance matrix of those two sets of vectors. This can be done, as Jason says, using Outer. It can also be done using the built-in function DistanceMatrix:

dm1 = Outer[EuclideanDistance, dalist, frameCenters, 1];
dm2 = DistanceMatrix[dalist, frameCenters];
dm1 == dm2

True

The same can also be achieved using Table:

Table[
 EuclideanDistance[u, v],
 {u, dalist},
 {v, frameCenters}
 ]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you both! It is solved. But Mathematica has hundreds of these functions. How someone knows if these functions are available to use. It is not possible to find the required function with just a simple googling, I believe. That is why I thought maybe using basic functions are handier. $\endgroup$ – Mehdi Ebadi Jun 8 at 16:27
  • $\begingroup$ Still, if someone can modify the function is really appreciated. $\endgroup$ – Mehdi Ebadi Jun 8 at 16:28
  • 1
    $\begingroup$ @MehdiEbadi I added an example using Table, which I think is perhaps the easiest to understand solution. Your way is not really a natural way of doing it. You would have to have another Map in there to map over both lists, not just one. You could have found these functions with Google if you had known about the name "distance matrix", I think, without it, it might be difficult. Outer and Table are both frequently used functions that you will learn by heart if you keep using Mathematica. $\endgroup$ – C. E. Jun 8 at 16:58
  • $\begingroup$ Thank you C.E.! $\endgroup$ – Mehdi Ebadi Jun 8 at 17:01
  • $\begingroup$ @MehdiEbadi It's my pleasure to help. $\endgroup$ – C. E. Jun 8 at 17:02

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.