find where two dataset intersect or get closer
5 次查看(过去 30 天)
显示 更早的评论
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.
![untitle.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223458/untitle.jpeg)
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.
![zoom.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223459/zoom.jpeg)
Does anyone know a function which find the closer point between two arrays or datasets?
Thanks a lot!
采纳的回答
Kevin Rawson
2019-6-7
编辑:Kevin Rawson
2019-6-7
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.
2 个评论
Kevin Rawson
2019-6-16
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!