[英]Find which line point belongs to - Sorting matrices corresponding
我在Matlab中有兩條線的幾何點數據。 我將它們導出到另一個程序,該程序通過這些點生成了樣條曲線。 例如,它計算樣條曲線隨機點的溫度,並將其發送回Matlab。
現在我有了這些數據,我也不知道溫度屬於哪條線。 但是我確實得到了新點的坐標。 因此,我需要確定這些點屬於哪條線,然后使用該信息將溫度矢量一分為二。
這是一個生成“示例”的代碼。
% Known geometric point data which is read by 3rd program.
x1 = 0:0.05:1; y1 = -sin(x1.*(4.*pi))./6;
x2 = 0:0.05:1; y2 = sin(x2.*(pi));
% 3rd program makes spline from given points.
xx1 = 0:0.075:1; xx2 = [0:0.1:1];
yy1 = spline(x1,y1,xx1);
yy2 = spline(x2,y2,xx2);
XY = [xx1, xx2; yy1, yy2];
[Y,I]=sort(XY(1,:));
% The program gives me DAT file with the 'new' coordinates of the new
% points. But the line-up of the points are random. In this example I've
% merged the coordinates of the two lines mixed them by sorting the X
% coordinates.
% The program gives me, for example, the temperature at these points in
% same order as the new coordinates. But now I'll need to know which line
% they belong to.
COORDINATE = XY(:,I);
TEMPERATURE = [COORDINATE(1,:); rand(1,length(COORDINATE))];
目標:
請注意,兩條線永遠不會交叉。 但是它們不必具有相同的x間距。
一種選擇是在MATLAB中DAT文件中的x坐標上進行樣條插值,並將結果y坐標與DAT文件中的y坐標進行比較。
% get xy coordinates
xi = COORDINATE(1,:);
yi = COORDINATE(2,:);
% spline interpolation for two lines of every x
yi1 = spline(x1,y1,xi);
yi2 = spline(x2,y2,xi);
% compare y coordinates
d1 = abs(yi1 - yi);
d2 = abs(yi2 - yi);
belongToLine1 = d1 <= d2;
belongToLine2 = d1 >= d2;
% plot
plot(COORDINATE(1,belongToLine1),COORDINATE(2,belongToLine1),'ob-');
hold on;
plot(COORDINATE(1,belongToLine2),COORDINATE(2,belongToLine2),'or-');
hold off
legend('line1','line2');
另一個選項(不需要插值,但有限制)是計算原始點和DAT文件中的點之間的成對距離:
% number of first line original points
n1 = length(x1);
% computing pairwise distance between the splines and original points
xy = [x1,x2;y1,y2]';
D = pdist2(COORDINATE',xy);
% find closest pair indexes
[~,idx] = min(D,[],2);
% determine membership
belongToLine1 = idx <= n1;
belongToLine2 = ~belongToLine1;
% plot
plot(COORDINATE(1,belongToLine1),COORDINATE(2,belongToLine1),'ob-');
hold on;
plot(COORDINATE(1,belongToLine2),COORDINATE(2,belongToLine2),'or-');
hold off
legend('line1','line2');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.