简体   繁体   English

Matlab中2D的顺序连接点

[英]Sequential connecting points in 2D in Matlab

I was wondering if you could advise me how I can connect several points together exactly one after each other. 我想知道您是否可以建议我如何将几个点彼此准确地连接在一起。

Assume: 假设:

data =
          x        y
      ------------------
      591.2990  532.5188
      597.8405  558.6672
      600.0210  542.3244
      606.5624  566.2938
      612.0136  546.6825
      616.3746  570.6519
      617.4648  580.4575
      619.6453  600.0688
      629.4575  557.5777
      630.5477  584.8156
      630.5477  618.5906
      639.2696  604.4269
      643.6306  638.2019
      646.9013  620.7697
      652.3525  601.1584

"data" is coordinate of points. “数据”是点的坐标。

Now, I would like to connect(plot) first point(1st array) to second point, second point to third point and so on. 现在,我想将第一个点(第一个数组)连接(绘制)到第二个点,第二个点连接到第三点,依此类推。

Please mind that plot(data(:,1),data(:,2)) will give me the same result. 请注意, plot(data(:,1),data(:,2))将给我相同的结果。 However, I am looking for a loop which connect (plot) each pair of point per each loop. 但是,我正在寻找一个循环,该循环会在每个循环中连接(绘制)每对点。

For example: 例如:

data1=data;
figure
scatter(X,Y,'.')
hold on
for i=1:size(data,1)
[Liaa,Locbb] = ismember(data(i,:),data1,'rows');
data1(Locbb,:)=[];

[n,d] = knnsearch(data1,data(i,:),'k',1);
x=[data(i,1) data1(n,1)];
y=[data(i,2) data1(n,2)];
plot(x,y);
end
hold off

Although, the proposed loop looks fine, I want a kind of plot which each point connect to maximum 2 other points (as I said like plot(x,y) ) 虽然,建议的循环看起来不错,但我想要一种绘图,每个点都连接到最多两个其他点(就像我说的plot(x,y)

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks for all of your helps, finally a solution is found: 感谢您的所有帮助,最后找到了一个解决方案:

n=1;
pt1=[data(n,1), data(n,2)];
figure
scatter(data(:,1),data(:,2))
hold on
for i=1:size(data,1)
    if isempty(pt1)~=1
        [Liaa,Locbb] = ismember(pt1(:)',data,'rows');
             if Locbb~=0
                data(Locbb,:)=[];
                [n,d] = knnsearch(data,pt1(:)','k',1);
                x=[pt1(1,1) data(n,1)];
                y=[pt1(1,2) data(n,2)];
                pt1=[data(n,1), data(n,2)];
                plot(x,y);
             end
    end
end
hold off

在此处输入图片说明

BTW it is possible to delete the last longest line as it is not related to the question, if someone need it please let me know. 顺便说一句,可以删除最后一个最长的行,因为它与问题无关,如果有人需要,请告诉我。

You don't need to use a loop at all. 您根本不需要使用循环。 You can use interp1 . 您可以使用interp1 Specify your x and y data points as control points . xy数据点指定为控制点 After, you can specify a finer set of points from the first x value to the last x value. 之后,您可以指定从第一个x值到最后一个x值的更精细的点集。 You can specify a linear spline as this is what you want to accomplish if the behaviour you want is the same as plot . 您可以指定linear样条曲线,因为如果所需的行为与plot相同,这就是您要完成的工作。 Assuming that data is a 2D matrix as you have shown above, without further ado: 假设data是如上所示的2D矩阵,无需费力地:

%// Get the minimum and maximum x-values
xMin = min(data(:,1));
xMax = max(data(:,1));
N = 3000; % // Specify total number of points

%// Create an array of N points that linearly span from xMin to xMax
%// Make N larger for finer resolution
xPoints = linspace(xMin, xMax, N);

%//Use the data matrix as control points, then xPoints are the values
%//along the x-axis that will help us draw our lines.  yPoints will be
%//the output on the y-axis
yPoints = interp1(data(:,1), data(:,2), xPoints, 'linear');

%// Plot the control points as well as the interpolated points
plot(data(:,1), data(:,2), 'rx', 'MarkerSize', 12);
hold on;
plot(xPoints, yPoints, 'b.');

Warning: You have two x values that map to 630.5477 but produce different y values. 警告:您有两个x值映射到630.5477但产生不同的y值。 If you use interp1 , this will give you an error, which is why I had to slightly perturb one of the values by a small amount to get this to work. 如果您使用interp1 ,这将给您一个错误,这就是为什么我必须稍微干扰其中一个值才能使其正常工作。 This should hopefully not be the case when you start using your own data. 希望当您开始使用自己的数据时,情况并非如此。 This is the plot I get: 这是我得到的情节:

在此处输入图片说明

You'll see that there is a huge gap between those two points I talked about. 您会发现我所说的这两点之间存在巨大差距。 This is the only limitation to interp1 as it assumes that the x values are strictly monotonically increasing. 这是对interp1的唯一限制,因为它假定x值严格单调递增。 As such, you can't have the same two points in your set of x values. 因此,在x值集中不能有相同的两个点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM