找到哪个线点属于 - 对应的排序矩阵

时间:2017-04-21 00:13:53

标签: matlab sorting

我在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. 确定COORDINATES的哪些点属于[x1,y1]或[x2,y2]。
  2. 将TEMPERATURE分割为[xx1; T1]和[xx2; T2]与#1相对应。
  3. 请注意,这两条线永远不会相互交叉。但他们没有必要具有相同的x间距。

1 个答案:

答案 0 :(得分: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