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

1 回表示 (過去 30 日間)
Lorenzo Balestra
Lorenzo Balestra 2019 年 11 月 11 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 12 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 11 日
P_stored(j) = min(max(lim_min, P_stored(j-1) + P_in_out(j)), lim_max);

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by