how to set the final value for a for loop

t=0.01
for i=1:82
t(i+1)=t(i)*(1.1)
if t > 24
t = 24;
end
end
The final value is 24.7, I want to make the last value to be set to 24 even if it's bigger than 24

回答 (1 件)

Voss
Voss 2022 年 3 月 24 日

0 投票

To make the last element of t equal to 24 if it's bigger than 24:
t=0.01;
for i=1:82
t(i+1)=t(i)*(1.1);
end
if t(end) > 24
t(end) = 24;
end
To make any element of t (except the first) equal to 24 if it's bigger than 24, as the loop iterates:
t=0.01;
for i=1:82
t(i+1)=t(i)*(1.1);
if t(end) > 24
t(end) = 24;
end
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2022 年 3 月 24 日

回答済み:

2022 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by