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

조회 수: 1 (최근 30일)
Majid
Majid 2022년 10월 17일
댓글: Majid 2022년 10월 17일
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