Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
3  
Try EuclideanDistance[x1,#]&/@y1! –  PlatoManiac Feb 2 '13 at 17:42
1  
You need to read up on Map. –  Szabolcs Feb 2 '13 at 17:51
1  
1  
@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
1  
Also related –  Leonid Shifrin Feb 2 '13 at 18:27
show 4 more comments

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.