MATLAB plot of a vector on a specific range

2019-01-28 22:08发布

问题:

I need to know how can i plot a vector over a specific interval? I'm using that to highlight this interval, by plotting the vector values with a different color. So Eventually, i will need to plot the vector as it is first with blue color for example, and then plot that specific interval with red color. Thanks for your time, Regards

回答1:

t = 0:0.01:8*pi;
y = sin(t);

If you want to plot a specific t interval in red then:

ind = t>2 & t<6;
plot(t,y);
hold on
plot(t(ind), y(ind), 'r')

If you want to plot a specific y interval in red then:

ind = y>0.5 & y<0.8;
plot(t,y);
hold on
plot(t(ind), y(ind), 'r')