1
$\begingroup$

I have 2 sets of data of points in 3D space. What I want to do is to find the minimum distance of pair of points from each set (so basically how close the sets can get to each other). For example, say

fst = {{2.22673,0.59492,2.6842},{3.13408,0.16181,1.63544}, 
       {1.34213,-0.31185,3.25682},{3.11765,-1.15956,1.2044},
       {3.21693,-1.46205,-0.21326},{3.25025,1.24013,0.66829},
       {3.34529,0.95071,-0.68805},{3.32838,-0.43039,-1.13854},
       {-0.45219,1.37427,3.23023},{0.47229,2.32191,2.63187}}
sec = {{9.22673,0.59492,2.6842},{10.1341,0.16181,1.63544},
       {8.34213,-0.31185,3.25682},{10.1177,-1.15956,1.2044},
       {10.2169,-1.46205,-0.21326},{10.2503,1.24013,0.66829},
       {10.3453,0.95071,-0.68805},{10.3284,-0.43039,-1.13854},
       {6.54781,1.37427,3.23023},{7.47229,2.32191,2.63187}}

What I did was to first create list of all pairs

perm = Outer[List, fst, sec]

Then I applied the list to function EuclideanDistance[]

Min[EuclideanDistance @@ perm]

This apparently didn't work, because the distance function doesn't know how to read the coordinates. Is there a way around this?

$\endgroup$
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Aug 31 '16 at 16:03
  • 2
    $\begingroup$ Min[Outer[EuclideanDistance, fst, sec, 1]] You need to specify the level (4th argument) in Outer. Check out the docs. $\endgroup$ – Quantum_Oli Aug 31 '16 at 16:05
  • 2
    $\begingroup$ For large point sets it is more efficient to work with Nearest, like so. In[22]:= nf1 = Nearest[fst]; candidates = nf1[sec][[All, 1]]; mindist = Min[MapThread[EuclideanDistance, {sec, candidates}]] Out[24]= 3.95815195623 $\endgroup$ – Daniel Lichtblau Aug 31 '16 at 16:25
  • $\begingroup$ Ah, good point. Thank you! $\endgroup$ – psimanjuntak Sep 1 '16 at 13:03
  • 1
    $\begingroup$ Alternatively: Min@DistanceMatrix[fst, sec]. $\endgroup$ – user31159 Sep 12 '16 at 23:53

Browse other questions tagged or ask your own question.