filloutliers

Detects outliers and replaces them with a chosen value.

Category: Data Import and Analysis > Preprocessing Data

Syntax

B = filloutliers(A, fillmethod)

Replaces outliers (found by the default median rule) according to fillmethod.

[B, TF] = filloutliers(___)

Also returns a logical array marking the replaced elements.

Description

By default an element is an outlier when it lies more than three scaled median absolute deviations from the median, the same rule used by isoutlier. filloutliers keeps the data length and edits only those elements, in contrast to rmoutliers, which deletes them.

Choose 'center' to pull outliers to the median, or 'previous'/'next' to hold the last good value, which suits time series.

Input arguments

A
Numeric vector or matrix.
fillmethod
'center' (the median), 'previous', 'next' or 'nearest' (copy a neighboring non-outlier), or 'linear'.

Output arguments

B
Array of the same size with outliers replaced.
TF
Logical array, true where an element was replaced.

Example

Hold the previous good value

x = [1 2 3 4 5 6 7 8 9 10 50];
filloutliers(x, 'previous')
filloutliers(x, 'center')
Output
ans = 1×11

     1     2     3     4     5     6     7     8     9    10    10
ans = 1×11

     1     2     3     4     5     6     7     8     9    10     6

Notes

With 'linear' an outlier at the end of the data is extrapolated, so 'previous' or 'center' give more predictable results there.

See also

isoutlier rmoutliers fillmissing movmedian