Replace all zeros in vector by previous non-zero value

后端 未结 6 1318
孤独总比滥情好
孤独总比滥情好 2020-12-09 16:31

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

6条回答
  •  生来不讨喜
    2020-12-09 16:46

    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].

提交回复
热议问题