I have vectors of equal lengths where I want to replace the zeroes (missing data) with their preceding values. So far I have written:
dt = {1, 0, 2, 0, 3};
MapThread[(dt[[#1]] = dt[[#2]]) &, {#, #-1}] & [Position[dt, 0]];
dt
{1, 1, 2, 2, 3}
I have 3 questions:
(1) Are there alternatives to the above code, which I don't like aesthetically ?
(2) How can I expand it to go over matrices of equal-length-vectors ?
(3) How can I deal with a zero in the first position (replace by the following element) ?