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