Replace all zeros in vector by previous non-zero value

后端 未结 6 1310
孤独总比滥情好
孤独总比滥情好 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:58

    Vector operations generally assume independence of the individual items. If you have a dependence on an earlier item, then looping is the best way to do it.

    Some extra background on matlab: In matlab the operations are typically faster not because of vector operations specifically, but because a vector operation simply does the loop in native C++ code instead of through the interpreter

提交回复
热议问题