# find where two dataset intersect or get closer

9 views (last 30 days)
fransec on 6 Jun 2019
Commented: Kevin Rawson on 16 Jun 2019
Dear all,
I have two dataset composed by two arrays, xydata and xyfunction. The first is a 10x2 matrix (data1), the second is a 300x2 matrix (data2). In the two columns of both dataset are the x and y coordinates. Since now I used without problem the function [xi,yi] = polyxpoly(x1,x2,y1,y2), but now the result is an empty matrix
>> whos xydata
Name Size Bytes Class Attributes
xydata 10x2 160 double
>> whos xyfunction
Name Size Bytes Class Attributes
xyfunction 300x2 4800 double
[xi,yi] = polyxpoly(xdata,ydata,xfunction,yfunction)
xi =
0×1 empty double column vector
yi =
0×1 empty double column vector
The problem is that, If i get closer and zoom, I can observe the really the two dataset don't cross each other. Does anyone know a function which find the closer point between two arrays or datasets?
Thanks a lot!

darova on 7 Jun 2019
\what if slightly replace data2?
fransec on 7 Jun 2019
Sorry, I don't understand your comment..

Kevin Rawson on 7 Jun 2019
Edited: Kevin Rawson on 7 Jun 2019
I'm not understanding how it's a problem if given your two datasets don't cross anywhere that polyxpoly is returning an empty set. It's returning exactly what it should.
If you want to know the closest points between the two datasets, use can use the min and pdist2 functions.
If you want to know the minimum distance between a point of one dataset with a line segment of the other, that's a different problem, one that would have to be examined for 299 x 10 + 300 x 9 cases.

fransec on 7 Jun 2019
You're right, no problem in the polyxpoly function, it returns an empty set because there is no cross really.
I am trying to use pdist2 function, but i need the indices of the closest point and at the moment I can't find them. Consider that data 1(my data) and data2(the line) have different length...
Kevin Rawson on 16 Jun 2019
From your example, pdist2 will return a 10x300 array, which by default will contain the Eucliedean distance between every data point in data1 and data2.
If you then evaluate:
[minColValues, minRowIndices] = min(pdist2(data1, data2));
[minVal, minIndex] = min(minColValues);
You will have the closest points to one another:
data1(minRowIndices(minIndex), :)
data2(minIndex, :)
and the distance between them:
minVal
But as I mentioned previously, you might also need/want to evaluate the minimum distance between the line segment between two data points of dataset 1, and a point in dataset 2.