Write a loop that sums and subtracts elements with a lower and upper limit?

1 visualización (últimos 30 días)
Hello,
I have an array that is composed by positive and negative numbers:
P_in_out = [ 1.1 2.2 2.4 3.6 2.4 -3.5 -.6 2 ....]
I have a starting value P and i am addind and subtracting the values
for j = 1 : length (P_in_out)
if j == 1
P_stored(j) = P * 0.5;
else
P_stored(j) = P_stored(j-1) + P_in_out(j);
end
end
The thing i cannot implement is that the value should stop adding values to P_stored once the value lim_max = 500 is reached and should stop subtracting values when the limiti lim_min = 0 is reached.
The loop should not stop when the limit is reached, just stop adding values and write P_stored(j) = lim_max or P_stored(j) = lim_min according to the limit reached.
any idea?
  3 comentarios
Walter Roberson
Walter Roberson el 11 de Nov. de 2019
If the value would go from 499 to 501, then should it be not added? Should only as much as needed to reach 500 be added?
Once the limit is reached, is the rule that all further values are to be ignored? Or is the rule that you could add negative values to bring it below 500 again, at which point the next positive value could be added (until the limit) and so on?
Lorenzo Balestra
Lorenzo Balestra el 11 de Nov. de 2019
Editada: Lorenzo Balestra el 11 de Nov. de 2019
You could add negative values to bring it below 500 again, and they will be subtrtacted untill 0 is reached. After that the negative values are also to be ignored. Once the sign changes again in P_in_out we can bring the values up from 0 to any value, unless is below 500 and so on. Hope is more clear.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 11 de Nov. de 2019
Do you want to stop the loop entirely or do you want to saturate and continue on with the next iteration starting from the saturated value? So if I had:
x = [400 90 9 2 -3 -500]
do you want your program to compute the sums 400, 490, 499, 501 and stop before adding the -3, or do you want it to compute the sums 400, 490, 499, 500, 497, 0 by treating the 501 as 500 and what would have been -3 as 0?
If the former, use break as KALYAN ACHARJYA showed or use cumsum and find the first out-of-bounds value to know where you should stop.
If the latter, use min and max around your addition code.

Más respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 11 de Nov. de 2019
Editada: KALYAN ACHARJYA el 12 de Nov. de 2019
The thing i cannot implement is that the value should stop adding values to P_stored once the value lim_max = 500 is reached and should stop subtracting values when the limiti lim_min = 0 is reached.
P_stored(1)=P*0.5;
for j=1:length (P_in_out)
P_stored(j) = P_stored(j-1) + P_in_out(j);
if lim_max=>500 || lim_min==0
break
end
end
Is this? you may avoid loop also.
  1 comentario
Walter Roberson
Walter Roberson el 11 de Nov. de 2019
No, in that code whether lim_max is >= 500 or == 0 is known ahead of time, so it does not make sense to test it in the loop, and lim_min=0 is wrong syntax for an if. You should probably be testing P_stored(i) < lim_min || P_stored(i) >= lim_max

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by