How to substitute some elements from a vector

1 Ansicht (letzte 30 Tage)
Viridiana  Torres
Viridiana Torres am 10 Mai 2016
Beantwortet: Andrei Bobrov am 10 Mai 2016
Hello everyone!
If i have the next vector Z=[10;10;10;11;11;13;13] which is associated to another vector: X=[1;6;65;34;21;73;14] a nd I want to create a third vector, Y, with almost all the elements in X, but just replacing a 0 in X BEFORE the element (i,j) from Z changes. How can I do this? In this example my desire outcome would be the next vector Y=[1;6;0;34;0;73;14]
Thanks a lot for your help!

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 10 Mai 2016
Y = X;
Y([diff(Z(:))~=0;false]) = 0

Weitere Antworten (1)

Star Strider
Star Strider am 10 Mai 2016
Bearbeitet: Star Strider am 10 Mai 2016
This works:
Z=[10;10;10;11;11;13;13];
X=[1;6;65;34;21;73;14];
idx = diff([Z; 0]) > 0; % Logical Vector Detecting Transitions
Y = zeros(size(X));
Y(~idx) = X(~idx)
Y =
1
6
0
34
0
73
14
EDIT — Added output result for Y.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by