Hi, I have an nx2 matrix, the first column gives the order, 10 20 30 40 and then repeats again, but if there is no observation it jumps to the next (eg if no 40 then it has 10 20 30 10 20 30 40 ....)
What I need is to repeat what is in the previous row in the case where there is no last (40) or next to last (30):
A) If there is no 40 but there is 30, then create row 40 and repeat element of 30 in the second column.
B) If there is no 40 and no 30 but there is 20, then create row 30 and 40 and repeat element of 20 in the second column.
C) If there is no 30 but there is 20, then creaste row 30 and repeat element of 20 in the second column.
I give an example with matrix M1 below for the 3 cases above.
Original matrix M1 is 12x2
M1 = [
10 44;
20 72;
30 21;
40 13;
10 32;
20 26;
30 55;
10 23;
20 98;
10 36;
20 84;
40 22]
New matrix M2 is 16x2
M2 = [
10 44;
20 72;
30 21;
40 13;
10 32;
20 26;
30 55;
40 55;
10 23;
20 98;
30 98;
40 98;
10 36;
20 84;
30 84;
40 22]
In M2 since the first cycle 10 20 30 40 in M1 was ok no additions.
But the second cycle in M1 has no 40, so M2 creates the row 40 and repeats element of 30.
The third cycle has no 30 nor 40, but has 20, so creates 30 and 40 and repeats element of 20.
The fourth cycle has no 30 so creates 30 and repeats element of 20 (note 40 is not touched)
Help please