how to set the final value for a for loop
Ältere Kommentare anzeigen
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
am 24 Mär. 2022
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!