## Plotting while skipping the middle point in a vector

Hi Matlab Family.

I have to deal with this trouble with my code.

I have a matrix and i need to connect the points in every row. However, when there is a blank between two points ( "blank distance" >1), the code will skip the blank and continue to the end. It is quite easy to solve if i use my code below, draw point to point.

v = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];

c = [1];

figure()

[QQ,KK]=size(v);

s=1;

for n =1:KK-2;

if v(n+1)- v(n)== 1;

x= [(v(n)),(v(n+1))];

y= [(c(1)),(c(1))];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

elseif v(n+1) – v(n)> 1;

x= [v(n+1),v(n+2)];

y= [c(1),c(1)];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n_’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

end

end

The problem is that the code becomes very slow and when the number of rows increases, the point coordinate files that are extracted from the code are too many. So please help to optimize the code by connecting a point to the furthest point as it can while ignoring the middle points. Thanks so much! Have a good day.Hi Matlab Family.

I have to deal with this trouble with my code.

I have a matrix and i need to connect the points in every row. However, when there is a blank between two points ( "blank distance" >1), the code will skip the blank and continue to the end. It is quite easy to solve if i use my code below, draw point to point.

v = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];

c = [1];

figure()

[QQ,KK]=size(v);

s=1;

for n =1:KK-2;

if v(n+1)- v(n)== 1;

x= [(v(n)),(v(n+1))];

y= [(c(1)),(c(1))];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

elseif v(n+1) – v(n)> 1;

x= [v(n+1),v(n+2)];

y= [c(1),c(1)];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n_’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

end

end

The problem is that the code becomes very slow and when the number of rows increases, the point coordinate files that are extracted from the code are too many. So please help to optimize the code by connecting a point to the furthest point as it can while ignoring the middle points. Thanks so much! Have a good day. Hi Matlab Family.

I have to deal with this trouble with my code.

I have a matrix and i need to connect the points in every row. However, when there is a blank between two points ( "blank distance" >1), the code will skip the blank and continue to the end. It is quite easy to solve if i use my code below, draw point to point.

v = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];

c = [1];

figure()

[QQ,KK]=size(v);

s=1;

for n =1:KK-2;

if v(n+1)- v(n)== 1;

x= [(v(n)),(v(n+1))];

y= [(c(1)),(c(1))];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

elseif v(n+1) – v(n)> 1;

x= [v(n+1),v(n+2)];

y= [c(1),c(1)];

%data1xy=[x’ y’];

plot(x.*s,y.*s,’k’)

axis square

ylim([0 2])

xlim([0 35])

%dlmwrite([‘n_’,num2str(n),’.txt’],data1xy,’delimiter’,’t’);

hold on

end

end

The problem is that the code becomes very slow and when the number of rows increases, the point coordinate files that are extracted from the code are too many. So please help to optimize the code by connecting a point to the furthest point as it can while ignoring the middle points. Thanks so much! Have a good day. matlab, plot, point, matrix, optimize MATLAB Answers — New Questions