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

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

Thanks for your quick response! Fantastic, you made it look so easy. Adding NaN value is great.
It's very close. I will export the coordinates to a machine. However, this machine cannot read the NaN value. That is why I have to draw one by one point.
Could you show me how to extract the coordinate points separately for each line like the image I described below (extract the coordinate for each line into its txt file separately for example 4 lines are 4 files), and if the middle points of each part are removed to reduce the capacity, it will perfect (if it is too difficult, then it's okay, I can skip it).
Thanks so much for paying attention to my problem and I'm so sorry cause my problem is a bit special :D
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')
My god, it is exactly what i described, amazing. you made my day!
Thanks for your support!!!!

Sign in to comment.

More Answers (1)

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

2 Comments

Thanks so much for your suggestion.Absolutely, i will keep it in mind :D

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 3 Jul 2024

Commented:

on 3 Jul 2024

Community Treasure Hunt

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

Start Hunting!