Which Loop Should I use?

Hello I wish to set two triggering point for my programme. My current programme runs a for loop which using a series of Input variable A the programme is:
A=xlsread('1'); %(Variable A has 432 values)
B(1)=1;
for i=1:1:432
if A(i)>B(i)
C(i)=abs(A(i)-B(i));
if C(i)>2
D(i)=1;
B(i+1)=B(i)+D(i);
else
D(i)=0;
end
end
for first few A values are greater than 1. From above example, we can see that D=1 when C>2. I wish to alter the programme so that when First C>2, it would triggering D=1 and continue to output D=1 even C<2. But with an exiting triggering condition C<1. which when C<1, it would output D=0. The tricky part is the previous D value would be included into the next C value's calculation. I wonder which loop or condition should I use.
I wish I have made my self clear.
Many Thanks

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 5 日
編集済み: Andrei Bobrov 2017 年 7 月 5 日

0 投票

B = cumsum(A==5) & cumsum(A==2,'revers')
or
B = cumsum(A==5) & flipud(cumsum(flipud(A==2)))

この質問は閉じられています。

質問済み:

2017 年 7 月 5 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by