ending FOR loop and starting over
Mostrar comentarios más antiguos
Hi.
I am building a program where I am summing two values. one is just a number and another is an array
KAP = 5;
n = 1
BAT;
A = [5 10 -5 -7 12 -7 18];
for jj = 1:length(A)
BAT = KAP*n + A(jj)
if BAT < 0
n = n+1
end
end
So i dont know what to do so the value n would increase and also the program should start again at the beggining. So the BAT value is at the end never under 0. Can you help me please i tried with continue and also break and return but still i dont get the right resoult
1 comentario
Walter Roberson
el 12 de Jul. de 2022
Do I understand correctly that the desired output is:
- n = 1 if all A values are non-negative
- otherwise n = smallest integer such that n*5+A(jj) is non-negative for all jj?
If so then I would suggest to you that in finding n you only need to pay attention to min(A)
Respuesta aceptada
Más respuestas (1)
Bharat Chandra Mukkavalli
el 12 de Jul. de 2022
0 votos
Hi,
I have gone through your code, would you please explain the purpose of the if condition in the for loop?
In the code, the cumulative sum is not being stored and only the value of the n is being updated when the BAT value is less than zero. The final value stored in BAT is KAP*n + A(end).
1 comentario
Jernej Primozic
el 12 de Jul. de 2022
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!