Code covered by the BSD License  

Highlights from
FILLZERO (v2.1, may 2008)

5.0

5.0 | 2 ratings Rate this file 2 Downloads (last 30 days) File Size: 2.47 KB File ID: #19906

FILLZERO (v2.1, may 2008)

by Jos (10584)

 

13 May 2008 (Updated 26 Jun 2008)

replaces zeros of an array with preceeding non-zero values

| Watch this File

File Information
Description

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

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
14 May 2008 Urs (us) Schwarz

another typical josian utility snippet with exhaustive help including some good examples and a clean, innovative, optimized, and well-documented computational engine closely following TMW's philosophy of ND matrix manipulators...
simply said: a must-have for matrix acrobats...
us

23 Jun 2008 Harley Ingram

I found this function to be very useful both functionally and as an example of well written, innovative code. Thank you very much.

Please login to add a comment or rating.
Updates
20 May 2008

fixed small error when all elements were NaN or Inf

26 Jun 2008

fixed bug in higher dimensions and when input contained no zeros

26 Jun 2008

fixed third bug

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 10:01:02
fill Jos (10584) 24 Oct 2008 15:42:25
zero Jos (10584) 24 Oct 2008 15:42:25
expand Jos (10584) 24 Oct 2008 15:42:25
change Jos (10584) 24 Oct 2008 15:42:25
run length Jos (10584) 24 Oct 2008 15:42:25
utility Jos (10584) 24 Oct 2008 15:42:35
matrix Jos (10584) 24 Oct 2008 15:43:08

Contact us at files@mathworks.com