Match two vector and star the 2nd vector with zero value

Hi,
Vectro A has the values of MATH function.
I want to define a vector B that has the same value of A except that B start from zero. In other word, B (i) = 0 if A (i) is not zero until the first zero element comes. Then, B(i) = A(i) for the rest of the vector. where i is the index.
I tried to do this by for loop and if statment but it does not work:
for i = 1:length(Time);
if A(i) ~= 0
B(i)= 0;
j=i;
elseif A(i) == 0;
for j = i :length(Time1);
B(j)= A(j);
end
break
end
break
end
For example:
If
A = [ 0.6 1 0.4 0 -0.4 -1 -0.4 0.2 0.6 1]
I want
B = [ 0 0 0 0 -0.4 -1 -0.4 0.2 0.6 1]

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 11 de Mayo de 2014
Editada: Azzi Abdelmalek el 11 de Mayo de 2014
A = [ 0.6 1 0.4 0 -0.4 -1 -0.4 0.2 0.6 1]
idx=find(A==0,1)
B=A;
B(1:idx)=0

3 comentarios

Thank you very much, your answer is really helpful
Can you please tell me, How can I do it if I want vector B to start from the 2nd zero in A or the 3rd zero in A ....ect?
For example: A =[ 0.6 1 0.4 0 -0.4 -1 -0.4 0.2 0.6 1 0 -0.4 0.2 0.6]
B = [ 0 0 0 0 0 0 0 0 0 0 0 -0.4 0.2 0.6]
A =[ 0.6 1 0.4 0 -0.4 -1 -0.4 0.2 0.6 1 0 -0.4 0.2 0.6]
zero_position=2;
idx=find(A==0,zero_position);
idx=idx(end);
B=A;
B(1:idx)=0
idx = find(A==0, 2);
B = A;
B(1:idx(2)) = 0;

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Mayo de 2014

Comentada:

Jan
el 11 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by