how to set the final value for a for loop
Mostrar comentarios más antiguos
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
Respuestas (1)
Voss
el 24 de Mzo. de 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
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!