Computing the correct output from an if function

조회 수: 2 (최근 30일)
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 5월 4일
답변: Walter Roberson 2019년 5월 4일
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

답변 (1개)

Walter Roberson
Walter Roberson 2019년 5월 4일
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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by