## Computing the correct output from an if function

### Nikolaos Zafirakis (view profile)

on 4 May 2019
Latest activity Answered by Walter Roberson

### Walter Roberson (view profile)

on 4 May 2019
My code keeps showing an error. So the statement X_dot needs to start calculating from the second point in my measurements. so I'm using an if statement but i guess I am not using it correctly I want to say if t is = to 1 then output L otherwise if t is larger than one say 2 output the statement X_dot.
for i= 1:length(t)
if t == 1
L = [0 0 0]';
else t > 1
X_dot = (Bb(i) - Bb(i-1)) / (t(i) - t(i-1))
end
end

### Tags ### Walter Roberson (view profile)

on 4 May 2019

t is a vector. if t == 1 is the same as if all(t == 1) which is false because not all t values are 1.
else t > 1 is the same as
else
t > 1 %calculate and display logical vector showing which t are greater than 1
You should be using t(i)
On the other hand you are overwriting all of L and all of X_dot, so the effect is the same as if you had only done the iterations for t(i) == 1 and the last t(i) that is > 1.