Add value in vector and use it as input for the next row

1 view (last 30 days)
Hello!
I have a column vector S(M,1) containing "0" and "1". And i have another column vector E(M,1) containing values.
I want to use a condition :
if S(i,:)==1
E1(i,:)==E(i,:)+k %k is a fixed value
but this condition is used for the first "1" in S, because in the next "1" exisiting in S i will use this condition :
if S(i,:)==1
E2(i,:)=E(i,:)+E1 ;
it means that each time i will use the previous result.
for example
S=[1 1 1 0 1 0 ], E=[4 2 5 10 2 3]; k= 10
E_result=[14 16 21 0 23 0];
I'm asking if there is a way to do that?
thanks in advance.

Accepted Answer

Stephen23
Stephen23 on 17 Oct 2022
S = [1,1,1,0,1,0];
E = [4,2,5,10,2,3];
k = 10;
X = S==1;
Z = S;
Z(X) = k+cumsum(E(X))
Z = 1×6
14 16 21 0 23 0

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!