1

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  ``` 
C. E.
  • 70,533
  • 6
  • 140
  • 264
Mehdi Ebadi
  • 501
  • 2
  • 7
  • 2
    If dalist and frameCenters are both lists of {x,y} coordinates, you might use Outer[EuclideanDistance, dalist, frameCenters]. See Outer – Jason B. Jun 08 '20 at 15:42
  • 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. – Mehdi Ebadi Jun 08 '20 at 16:13

1 Answers1

3

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}
 ]
C. E.
  • 70,533
  • 6
  • 140
  • 264
  • 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. – Mehdi Ebadi Jun 08 '20 at 16:27
  • Still, if someone can modify the function is really appreciated. – Mehdi Ebadi Jun 08 '20 at 16:28
  • 1
    @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. – C. E. Jun 08 '20 at 16:58
  • Thank you C.E.! – Mehdi Ebadi Jun 08 '20 at 17:01
  • @MehdiEbadi It's my pleasure to help. – C. E. Jun 08 '20 at 17:02
  • I have a quick question C.E and appreciate your response. What does " level n in Listi" mean in the Outer function. – Mehdi Ebadi Jun 08 '20 at 17:39
  • @MehdiEbadi It's difficult to explain in a comment, there are some good answers here about the general concept of levels. – C. E. Jun 08 '20 at 18:50