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

Antworten (1)

Voss
Voss am 24 Mär. 2022

0 Stimmen

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 24 Mär. 2022

Beantwortet:

am 24 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by