2017-04-21 29 views
0

我在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))]; 

目標:

  1. 確定哪個座標的點屬於[X1,Y1]或[X2,Y2]。
  2. 拆分TEMPERATURE爲[xx1; T1]和[xx2; T2]相當於#1。

請注意,兩條線不會互相交叉。但是它們不需要具有相同的x間距。

回答

1

一種選擇是在MATLAB中的DAT文件的x座標上進行樣條插值,並將結果y座標與DAT文件中的座標進行比較。

% 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'); 

enter image description here

另一種選擇(不需要插值,但是是有限的)是計算在你的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'); 

enter image description here