I want to find the Euclidean distance between one point (x1) and a list of points (y1), which contains a lot of coordinates
x1 = killer[[2]]
{6.05102, 5.87667}
y1 = victim[[2 ;;]]
{{1.40687, 4.92494}, {0.419206, 1.70406}, {6.29657,0.577941}, {4.12022, 4.94952},
{2.04784, 5.94545}, {1.29192,1.43152}, {3.26737, 1.90134}, {4.27274, 0.528028},
{2.79659,1.37788}, {5.43955, 1.81355}}
Is it possible for me to find the EuclideanDistance between x1 and y1, where it will show all results between x1 and each elements in y1.
Answer
Here is what you want to do. Let the first point's coordinate be stored as a list of list as follows:
x1 = {{6.05102, 5.87667}}
and the second set of coordinates
y1 = {{1.40687, 4.92494}, {0.419206, 1.70406}, {6.29657,0.577941}, {4.12022, 4.94952},
{2.04784, 5.94545}, {1.29192,1.43152}, {3.26737, 1.90134}, {4.27274, 0.528028},
{2.79659,1.37788}, {5.43955, 1.81355}}
Now to compute the Euclidean distances between x1 and every element in y1 use Outer, your best friend from now on.
Outer[EuclideanDistance, x1, y1, 1]//Flatten
This then gives you
{4.74067, 7.00914, 5.30442, 2.14187, 4.00377, 6.51217, 4.85304, 5.63651, 5.55252, 4.10887}.
Hope this helps. In fact, you can loop this through various x1's as follows.
Table[Outer[EuclideanDistance, {killer[[k]]}, y1, 1], {k, 1, n}]
Where n is the Length of the killer list. This is fast and compact code.
Comments
Post a Comment