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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by