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

1 visualización (últimos 30 días)
Majid
Majid el 17 de Oct. de 2022
Comentada: Majid el 17 de Oct. de 2022
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.

Respuesta aceptada

Stephen23
Stephen23 el 17 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by