Bleed non-zeros to the right - MATLAB Cody - MATLAB Central

Problem 1998. Bleed non-zeros to the right

Difficulty:Rate

What a title! Yet, it says what I mean.

You get a vector, some values and a lot of zeroes. Every zero is replaced by the first non-zero element to its left.

For example, [1 0 0 2 0] becomes [1 1 1 2 2] and [0 0 -12 13 0 i 0 -i 76.54321 0 0] becomes [0 0 -12 13 13 i i -i 76.54321 76.54321 76.54321].

Easy, isn't it?

Regexp, regexprep, regexpi, eval, evalc, evalin, nor inline are allowed this time.

(This problem is related to http://www.mathworks.nl/matlabcentral/cody/problems/1999-bleed-non-zeros-to-the-right-using-regexp-and-that-sort-of-cheatcodess.

Solution Stats

62.5% Correct | 37.5% Incorrect
Last Solution submitted on Sep 03, 2025

Problem Comments

Solution Comments

Show comments
MATLAB EXPO 2025 Registration is Now Open! November 12 – 13, 2025
Registration is now open for MathWorks annual virtual event MATLAB EXPO 2025...
0
0

Problem Recent Solvers41

Suggested Problems

More from this Author31

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!