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

조회 수: 1 (최근 30일)
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
Walter Roberson
Walter Roberson 2019년 11월 11일
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 2019년 11월 11일
편집: Lorenzo Balestra 2019년 11월 11일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2019년 11월 11일
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.

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 11일
편집: KALYAN ACHARJYA 2019년 11월 12일
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
Walter Roberson
Walter Roberson 2019년 11월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by