FILLZERO (v2.1, may 2008)

replaces zeros of an array with preceeding non-zero values
2.3K Downloads
Updated 26 Jun 2008

View License

FILLZERO - replaces zeros of an array with preceeding non-zero values

Y = FILLZERO(X), when X is a vector, returns a vector Y in which each zero in X is changed into the last non-zero value of X that preceeds it. In other words, the sequence ".. A 0 .. 0 B .." is converted into the sequence ".. A A .. A B .."

For matrices, FILLZERO(X) operates on the first non-singleton dimension of X. FILLZERO(X,DIM) operates on the dimension DIM.

Examples:
X = [0 1 0 0 4 0 2 3 0 0 0 8 0] ;
Y = fillzero(X)
% --> Y = [0 1 1 1 4 4 2 3 3 3 3 8 8] ;

X = [1 2 3 0 ; 0 0 4 8; 0 5 0 0 ; 2 0 -2 0 ; 0 1 0 0]
fillzero(X) % ... equals fillzero(X, 1)
% -->
% 1 2 3 0
% 1 2 4 8
% 1 5 4 8
% 2 5 -2 8
% 2 1 -2 8

fillzero(X, 2)
% -->
% 1 2 3 3
% 0 0 4 8
% 0 5 5 5
% 2 2 -2 -2
% 0 1 1 1

FILLZERO can handle NaNs, and Infs. Example:
fillzero([1 0 NaN 0 3 0 Inf 0 -Inf 0 6 0])
% -> 1 1 NaN NaN 3 3 Inf Inf -Inf -Inf 6 6

See also FIND, CUMSUM

This function was inspired by a recent post on CSSM.

Version 2.1, may 2008

Cite As

Jos (10584) (2024). FILLZERO (v2.1, may 2008) (https://www.mathworks.com/matlabcentral/fileexchange/19906-fillzero-v2-1-may-2008), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Sparse Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0

fixed third bug