fillmissing2

Fills missing entries of a matrix using the surrounding values in two dimensions.

Category: Data Import and Analysis > Preprocessing Data

Syntax

B = fillmissing2(A, method)

Replaces each NaN in A using the chosen method.

B = fillmissing2(A, 'movmean', wlen)

Replaces each NaN with the mean of the non-missing values in a window of size wlen.

Description

Unlike fillmissing, which treats each column or row as a separate series, fillmissing2 looks at neighbors in both directions, so it suits images, maps and other gridded data. With 'movmean' a window is centered on every gap and the mean of the values present inside it is used; 'movmedian' uses the median instead and resists outliers.

Values that were not missing are left unchanged.

Input arguments

A
Numeric matrix with NaN marking the missing entries.
method
'nearest', 'movmean', 'movmedian', 'linear', 'natural' or 'v4'.
wlen
Window length (scalar for a square window, or [rows cols]) for the moving methods.

Output arguments

B
Matrix of the same size as A with the gaps filled.

Example

Mean of a 3 by 3 neighborhood

A = [1 NaN 3; 4 5 6; 7 8 9];
B = fillmissing2(A, 'movmean', 3)
Output
B = 3×3

    1.0000    3.8000    3.0000
    4.0000    5.0000    6.0000
    7.0000    8.0000    9.0000

Notes

MatLivre supports 'nearest', 'movmean', 'movmedian', 'linear', 'natural' and 'v4'. The 'cubic' method reports that it is not supported in this version. Calling it with only the matrix is an error.

See also

fillmissing ismissing rmmissing smoothdata2