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

Answers (1)

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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 24 Mar 2022

Answered:

on 24 Mar 2022

Community Treasure Hunt

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

Start Hunting!