Plotting while skipping the middle point in a vector

3 views (last 30 days)
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.
clc
clear all
close all
tic
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
timeElapsed = toc
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.

Accepted Answer

Alan Stevens
Alan Stevens on 3 Jul 2024
Something like this?
tic
v1 = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];
dv = diff(v1);
ix = find(abs(dv)>1);
v = []; s2 = 0;
for i = 1:length(ix)
s1 = s2+1; s2 = ix(i);
v = [v, v1(s1:s2),NaN];
end
v = [v, v1(ix(i)+1:end)];
c = ones(size(v));
figure()
plot(v,c)
toc
Elapsed time is 0.135112 seconds.
  3 Comments
Alan Stevens
Alan Stevens on 3 Jul 2024
Do you mean something like this?
v1 = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];
dv = diff(v1);
ix = find(abs(dv)>1);
s2 = 0;
for i = 1:length(ix)
s1 = s2+1; s2 = ix(i);
v = [v1(s1) v1(s2)];
filename = ['vfile' int2str(i) '.txt'];
save(filename, 'v', '-ascii')
end
v = [v1(ix(i)+1) v1(end)];
filename = ['vfile' int2str(i+1) '.txt'];
save(filename, 'v', '-ascii')
Hieu
Hieu on 3 Jul 2024
My god, it is exactly what i described, amazing. you made my day!
Thanks for your support!!!!

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 3 Jul 2024
hello
if you can avoid update the plot inside the for loop
here I store your data in xx and yy arrays and after the for loop you do the plot
it's more than 10x faster than your original code
also preallocating memory will speed up things especially if you are working with larger arrays (instead of concatenate and make an array grow dynamically in the for loop)
tic
v = [1 2 3 4 5 8 9 10 20 21 22 30 31 32];
c = [1];
[QQ,KK]=size(v);
s=1;
% preallocate memory
xx= zeros(1,2*(KK-2));
yy= xx;
for n =1:KK-2
if v(n+1)- v(n)== 1
x= [(v(n)),(v(n+1))];
y= [(c(1)),(c(1))];
elseif v(n+1) - v(n)> 1
x= [v(n+1),v(n+2)];
y= [c(1),c(1)];
end
% store the results
xx(1,n:n+1)= x;
yy(1,n:n+1)= y;
end
% remove duplicates (optionnal)
[xx,ia,ic] = unique(xx);
yy = yy(ia);
% do one plot call here
figure()
plot(xx.*s,yy.*s,'*k')
axis square
ylim([0 2])
xlim([0 35])
%dlmwrite(['n',num2str(n),'.txt'],data1xy,'delimiter','\t');
timeElapsed = toc
timeElapsed = 0.1244

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!