【问题标题】:Find which line point belongs to - Sorting matrices corresponding查找哪个线点属于 - 排序矩阵对应
【发布时间】:2017-04-21 14:30:19
【问题描述】:

我在 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 间距。

【问题讨论】:

    标签: matlab sorting


    【解决方案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');
    

    另一个选项(不需要插值但受到限制)是计算原始点与 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2011-02-10
      相关资源
      最近更新 更多