Iman
Iman

Reputation: 412

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. 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))

Any help would be greatly appreciated!

Upvotes: 1

Views: 6562

Answers (2)

Iman
Iman

Reputation: 412

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

enter image description here

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.

Upvotes: 1

rayryeng
rayryeng

Reputation: 104464

You don't need to use a loop at all. You can use interp1. Specify your x and y data points as control points. After, you can specify a finer set of points from the first x value to the last x value. You can specify a linear spline as this is what you want to accomplish if the behaviour you want is the same as plot. Assuming that data is a 2D matrix as you have shown above, without further ado:

%// 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. 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. This should hopefully not be the case when you start using your own data. This is the plot I get:

enter image description here

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. As such, you can't have the same two points in your set of x values.

Upvotes: 0

Related Questions