Code initiates again after exiting while loop which is within an if statement
Mostrar comentarios más antiguos
i = 2
for i = 2:523
if Storage(i-1) >= Stage_1_trigger
Outflow = Demand_and_losses
if Storage(i-1) + Flow(i) - Outflow < 0
Storage(i) = 0
elseif Storage(i-1) + Flow(i) - Outflow > Initial_storage
Storage(i) = Initial_storage
else
Storage(i) = Storage(i-1) + Flow(i) - Outflow
end
Storage(i)
i = i + 1
else
while Storage(i-1)<= Stage_1_exit
Outflow = 0.9*Demand_and_losses
if Storage(i-1) + Flow(i) - Outflow < 0
Storage(i) = 0
elseif Storage(i-1) + Flow(i) - Outflow > Initial_storage
Storage(i) = Initial_storage
else
Storage(i) = Storage(i-1) + Flow(i) - Outflow
end
i = i + 1
end
end
end
Hi, I have this code above. However, when the code exits the while loop, the i value initiates again.
For example, before entering the while loop, i = 5. When exiting the while loop, i = 10. However, when the code finishes with the while loop and goes back to the first for loop, i = 5 again rather than 10. How can I prevent this? Thanks!
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 8 de Jul. de 2020
0 votos
I gave a detailed explanation of how for loops work in https://www.mathworks.com/matlabcentral/answers/356884-how-do-i-convert-this-for-loop-into-a-while-loop#answer_281739
1 comentario
Arielle Patapanian
el 8 de Jul. de 2020
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!