Carry prior cell forward until there is a new value

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

It would be very helpful to have a small, representative vector where you show us the input, and the desired output.
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.
Thanks for looking into it, its something like this..
s = zeros(size(data_table));
s(var1>=2&var2<=10)=1;
s(var3>=20|var4>=46.53)=-1;
Output
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.
Tables
A B C D
12 11 6 1
3 5 18 30
4 6 17 20
1 8 5 8
12 15 25 9
1 2 5 9
11 12 19 25
12 15 16 20
9 15 5 8
1 2 8 2
1 1 15 40
8 9 19 45
15 16 17 45
7 9 5 10
0 8 16 18
4 5 19 33
1 0 15 50
0 15 19 48
1 1 16 20
1 20 18 40
s = zeros(size(A));
s(A>=2&B<=10)=1;
s(C>=20|D>=46.53)=-1;
Output
S
0
1
1
0
-1
0
0
0
0
0
0
1
0
1
0
1
-1
-1
0
0
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]
S = 1×20
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
S = 1×20
0 1 1 1 1 -1 0 0 0 0 0 0 1 1 1 1 1 -1 0 0
no issue with loop, let me try!
Thats it, thanks so much for taking the time to help me out!

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2020a

Richiesto:

IDN
il 26 Nov 2021

Commentato:

IDN
il 27 Nov 2021

Community Treasure Hunt

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

Start Hunting!

Translated by