I have the following data.

x1coord = Sort@RandomReal[{-2, 25}, 270]; y1coord = Sort@RandomReal[{-2, 25}, 219];

x2coord = Sort@RandomReal[{0, 27}, 280]; y2coord = Sort@RandomReal[{0, 27}, 224];

dataSet1 = Table[{x1, y1}, {x1, x1coord}, {y1, y1coord}];  
dataSet2 = Table[{x2, y2}, {x2, x2coord}, {y2, y2coord}];

I need to find nearest points of dataset1 and dataset2. Nearest point mean {x1, y1} to {x2, y2} so taking account of both point (not just the x or y in isolation). Note dataset2 has more points, any points left over in dataset2 can be discarded. The points discarded could be at either end of the list (low or high numbers).

Where the letters a, b etc indicate the nearest paired points.

So I need

DataSet3 = { {{x1a,y1a}, {x2a, y2a}}, {{x1b,y1b}, {x2b, y2b}}, .......etc}}

I am still using Mathematica 9, but plan to upgrade soon.

  • $\begingroup$ Have you seen Nearest? Have you tried anything yourself? $\endgroup$
    – MarcoB
    May 13, 2016 at 15:53
  • $\begingroup$ {#, First@Nearest[dataSet2, #]} & /@ dataSet1 $\endgroup$
    – N.J.Evans
    May 13, 2016 at 16:09
  • $\begingroup$ note it can happen that a given elements of set 2 is Nearest more than one point in set1. You need to specify how to handle that. I think this is a dup per Summit, in any case. $\endgroup$
    – george2079
    May 13, 2016 at 16:16


Browse other questions tagged or ask your own question.