Matlab/Octave algorithm example:
input vector: [ 1 0 2 0 7 7 7 0 5 0 0 0 9 ] output vector: [ 1 1 2 2 7 7 7 7 5 5 5 5 9 ]
The algorithm is
New in MATLAB R2016b: fillmissing, it does exactly as described in the question:
in = [ 1 0 2 0 7 7 7 0 5 0 0 0 9 ]; in(in==0) = NaN; out = fillmissing(in,'previous');
[This new functionality discovered in this duplicate question].