MATLAB Answers

0

Computing the correct output from an if function

Asked by Nikolaos Zafirakis on 4 May 2019
Latest activity Answered by Walter Roberson
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

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
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.

  0 Comments

Sign in to comment.