Superman
Superman

Reputation: 135

MATLAB conditional plotting

So I would like to plot a sine on MATLAB such that when |sin(x)| <= 1/2. Right now, here is my code:

figure(1)
t= [0:0.01:2*pi];
x = sin(t);
indices1 = find(abs(x)< 0.5);
plot(t(indices1), x(indices1));
ylim([-1, 1])

Output:

enter image description here

Is there a way to plot this such that the segments don't connect? Like, the first segment in which the sine function is within than 1/2 is connecting to the other segment in which it is within 1/2.

Upvotes: 1

Views: 127

Answers (1)

bla
bla

Reputation: 26069

assign NaN in the places you dont want to plot, for example

x2=x;
x2(abs(x)>0.5)=NaN;
plot(t, x2);

Upvotes: 2

Related Questions