Creating ramp signal from -1 to 1

I am trying to create a linear line from y = -1 to 1 from x = 0 to 2000 but my code is creating the linear line from y = 0 to 1. Where am I going wrong with this?
y = 167
t = 60
x = linspace(0, t, y*t);
rampIdx = 2000;
s4 = zeros(1, length(x));
s4(rampIdx+1:end)=1;
s4(1:rampIdx+1) = -1;
s4(1:rampIdx+1) = (1:rampIdx)/rampIdx

 Accepted Answer

Yongjian Feng
Yongjian Feng on 27 Jul 2021
Edited: Yongjian Feng on 27 Jul 2021
First there is an error in the last line:
y = 167;
t = 60;
x = linspace(0, t, y*t);
rampIdx = 2000;
s4 = zeros(1, length(x));
s4(rampIdx+1:end)=1;
s4(1:rampIdx) = -1;
z = linspace(-rampIdx, rampIdx, rampIdx);
s4(1:rampIdx) = z/rampIdx;
plot(x, s4)

4 Comments

Thanks but the line still isn't linear going from y = -1 to 1.
Modified. Please try again.
Thanks but it is not exactly what I am looking for. I have attached an image of what I am looking for.
Your boundary overlapped. Fixed again.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!