Computing the correct output from an if function

2 views (last 30 days)
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

Answers (1)

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

Categories

Find more on Programming 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!