Carry prior cell forward until there is a new value
Afficher commentaires plus anciens
I have a vector with possible 1,0,-1 as answers, each cell is the output of a statement/function. What I want is the following....If prior row is 0, then 1 can occur, and after 1 occurs i need it to continue populating rows below until statement outputs a -1 (also therefore -1 can only occur if prior row is 1).
8 commentaires
the cyclist
le 26 Nov 2021
It would be very helpful to have a small, representative vector where you show us the input, and the desired output.
dpb
le 26 Nov 2021
Concur w/ @the cyclist
If this is a static vector that contains the set of [1, 0, -1] that is one thing; if the function is supposed to be generating these as its results based on prior input, that's something else.
IDN
le 27 Nov 2021
dpb
le 27 Nov 2021
OK, I think... :) Now to make it even easier for us, the input vector as either cut-n-paste text (its short). Use code format button and then we can just copy it -- or run in online.
IDN
le 27 Nov 2021
Is there any issue just using a loop?
S = [0 1 1 0 0 -1 0 0 0 0 0 0 1 0 1 0 1 -1 -1 0]
for k = 2:numel(S)
m = [(S(k-1)==1) (S(k)~=-1)];
if all(m)
S(k) = 1;
elseif all(~m)
S(k) = 0;
end
end
S
IDN
le 27 Nov 2021
IDN
le 27 Nov 2021
Réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
