Carry prior cell forward until there is a new value
Mostra commenti meno recenti
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 Commenti
the cyclist
il 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
il 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
il 27 Nov 2021
dpb
il 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
il 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
il 27 Nov 2021
IDN
il 27 Nov 2021
Risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
