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

1 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2022-10-17
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by