# How do I find the Euclidean distance between one point and all the points in a list?

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.

-
Try EuclideanDistance[x1,#]&/@y1! – PlatoManiac Feb 2 '13 at 17:42
You need to read up on Map. – Szabolcs Feb 2 '13 at 17:51
– Yves Klett Feb 2 '13 at 18:12
@PlatoManiac If efficiency is important, one can gain an order of magnitude speedup by using Sqrt[Total[(x1 - #)^2]] & /@ y1 instead, due to auto-compilation (EuclideanDistance is not compilable). Slightly faster still can be a vectorized solution like Sqrt[Total[(Transpose[y1] - x1)^2]]. – Leonid Shifrin Feb 2 '13 at 18:27
Also related – Leonid Shifrin Feb 2 '13 at 18:27

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.

-
Just a suggestion: formatting code inline with text makes copy&pasting less convenient compared to separate code lines. – Yves Klett Feb 2 '13 at 20:38
@Yves, sorry I'm new to StackExchange, I'll try to edit it. – RunnyKine Feb 2 '13 at 20:58
You don't even need the Table. Just put the entire list of x1 into Outer. – Jens Feb 2 '13 at 21:01
No worries whatsoever, welcome to the party! – Yves Klett Feb 2 '13 at 21:09
@Jens, yes you're right. In this case you don't. But suppose you want to do further analysis of a large data set, like calculate the Max or Min of the distances from each of the x1's, then the table definitely comes in handy for Memory conservation. Otherwise Mathematica will store all of those unnecessary data in memory, because Outer will have to finish before you can Map "Min" or "Max" to the entire List. – RunnyKine Feb 2 '13 at 21:10

I understand that this question already has an accepted answer, but I couldn't resist to post my answer. this answer is mainly useful if you have a very large dataset. I define the following function using Compile function in Mathematica.

distance = Compile[{{n, _Real, 1}, {z, _Real, 2}},
Sqrt[Total[(# - n)^2]] & /@ z, RuntimeOptions -> "Speed",
Parallelization -> True, CompilationTarget -> "C",
RuntimeAttributes -> {Listable}
];


I tend to map the function over a two dimensional set with 10000000 (=$10^7$) points.

data = RandomReal[{0, 1}, {10000000, 2}];


The result is:

new[{0, 0}, data] // AbsoluteTiming // First
(* 0.609684 s *)


which is 10 times faster compared to the accepted answer:

Outer[EuclideanDistance, {{0, 0}}, data, 1] // AbsoluteTiming // First
(* 5.927118 s *)

-