replacing values from a separate vector to multiple repeated values in another vector

I have a vector in the form of
x = [1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 1 0];
and another vector resulted in from data manipulation,
y = [7.83 9 3.2 15.2 3.3];
I want these two vectors to produce another vector (replacing all 0s),
z = [0 7.83 7.83 7.83 7.83 7.83 7.83 0 9 0 3.2 3.2 3.2 3.2 0 0 15.2 15.2 0 3.3];
I started the code by getting the complement of vector x.
Appreacite any help.

 Respuesta aceptada

index=find(x==0);
idx=find(diff(index)~=1);
z=x;
z(z==0)=repelem(y,[idx(1) diff(idx) numel(index)-idx(end)]);
z(z==1)=0

Más respuestas (0)

Categorías

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

Productos

Versión

R2016a

Preguntada:

el 13 de Feb. de 2019

Comentada:

el 13 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by