I am trying to make a piecewise vector and have 10001 input values and should be getting 10001 output values, but am only getting one output value

1 view (last 30 days)
I am making a piecewise vector. I have 10001 t values and want to have the corresponding 10001 position values, but for some reason when I run the script I only get one position value. Does anyone know why this is happening and what I should do to fix it?
Here is my script:
t=-5:0.001:5; %time in milliseconds (ms)
if t<=-1; %ms
position=-t; %position in feet
elseif (t>-1)&(t<=1); %ms
position=t.^2; %ft
else 1<t; %ms
position=sin(t-1)/(t-1);%ft
end

Answers (1)

Walter Roberson
Walter Roberson on 2 Feb 2020
t = vector
position = zeros(size(t))
mask = t<=-1
position(mask) = -t(mask) ;
mask = t>-1&t<=1;
position(mask) = t(mask).^2 ;
And so on

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!