Moving median absolute deviation
M = movmad( returns
an array of local
k-point median absolute deviations
(MADs), where each MAD is calculated over a sliding window
k across neighboring elements of
the same size as
k is odd, the window is centered about
the element in the current position. When
even, the window is centered about the current and previous elements.
The window size is automatically truncated at the endpoints when there
are not enough elements to fill the window. When the window is truncated,
the MAD is taken over only the elements that fill the window.
Ais a vector, then
movmadoperates along the length of the vector.
Ais a multidimensional array, then
movmadoperates along the first array dimension whose size does not equal 1.
M = movmad(___, computes
the MAD along dimension
dim for any of the previous
syntaxes. For example,
movmad(A,k,2) for a matrix
across the columns of
A, computing the
sliding MAD for each row.
M = movmad(___, specifies
whether to include or omit
NaN values from the
calculation for any of the previous syntaxes.
NaN values in the calculation, which is the
movmad(A,k,'omitnan') ignores them and
computes the MAD over fewer points.
Centered Moving MAD of Vector
Compute the three-point centered moving MAD of a row vector. When there are fewer than three elements in the window at the endpoints, compute over the elements that are available.
A = [1 2 4 -1 -2 -3 -1 3 2 1]; M = movmad(A,3)
M = 1×10 0.5000 1.0000 2.0000 1.0000 1.0000 1.0000 2.0000 1.0000 1.0000 0.5000
Trailing Moving MAD of Vector
Compute the three-point trailing moving MAD of a row vector. When there are fewer than three elements in the window at the endpoints, compute over the elements that are available.
A = [1 2 1 -1 -2 -3 -1 3 4 1]; M = movmad(A,[2 0])
M = 1×10 0 0.5000 0 1.0000 1.0000 1.0000 1.0000 2.0000 1.0000 1.0000
Moving MAD of Matrix
Compute the 3-point centered moving MAD for each row of a matrix. The dimension argument is 2, which slides the window across the columns of
A. The window starts on the first row, slides horizontally to the end of the row, then moves to the second row, and so on.
A = [1 2 1; -1 -2 -3; -1 3 4]
A = 3×3 1 2 1 -1 -2 -3 -1 3 4
M = movmad(A,3,2)
M = 3×3 0.5000 0 0.5000 0.5000 1.0000 0.5000 2.0000 1.0000 0.5000
Moving MAD of Vector with
Compute the three-point centered moving MAD of a row vector containing two
A = [2 1 NaN -1 -2 -3 NaN 3 4 1]; M = movmad(A,3)
M = 1×10 0.5000 NaN NaN NaN 1.0000 NaN NaN NaN 1.0000 1.5000
Recalculate the moving MAD omitting the
NaN values. When
NaN elements, it computes over the remaining elements in the window.
M = movmad(A,3,'omitnan')
M = 1×10 0.5000 0.5000 1.0000 0.5000 1.0000 0.5000 3.0000 0.5000 1.0000 1.5000
Sample Points for Moving MAD
Compute a 3-hour centered moving MAD of the data in
A according to the time vector
A = [4 8 6 -1 -2 -3]; k = hours(3); t = datetime(2016,1,1,0,0,0) + hours(0:5)
t = 1x6 datetime Columns 1 through 3 01-Jan-2016 00:00:00 01-Jan-2016 01:00:00 01-Jan-2016 02:00:00 Columns 4 through 6 01-Jan-2016 03:00:00 01-Jan-2016 04:00:00 01-Jan-2016 05:00:00
M = movmad(A,k,'SamplePoints',t)
M = 1×6 2.0000 2.0000 2.0000 1.0000 1.0000 0.5000
Return Only Full-Window MADs
Compute the three-point centered moving MAD of a row vector, but discard any calculation that uses fewer than three points from the output. In other words, return only the MADs computed from a full three-element window, discarding endpoint calculations.
A = [1 2 1 -1 -2 -3 -1 3 4 1]; M = movmad(A,3,'Endpoints','discard')
M = 1×8 0 1 1 1 1 2 1 1
A — Input array
vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array.
k — Window length
numeric or duration scalar
Window length, specified as a numeric or duration scalar. When
a positive integer scalar, the centered MAD includes the element in
the current position plus surrounding neighbors. For example, a three-point
MAD defined by a window of length three results in the following calculation
for a vector
[kb kf] — Directional window length
numeric or duration row vector containing two elements
Directional window length, specified as a numeric or duration
row vector containing two elements. When
positive integer scalars, the calculation is over
The calculation includes the element in the current position,
before the current position, and
kf elements after
the current position. For example, a four-point MAD defined by the
[2 1] results in the following
calculation for a vector
dim — Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.
Consider a matrix
dim = 1, then
movmad(A,k,1)starts with the first column and slides vertically over each row. The MAD is computed over
kelements at a time. Then it moves to the second column and repeats the computation. This process continues until all columns are exhausted.
dim = 2, then
movmad(A,k,2)starts with the first row and slides horizontally across each column, computing over
kelements at a time. Then it moves to the second row and repeats the computation. This process continues until all rows are exhausted.
'includenan' (default) |
NaN condition, specified as one of these
NaNvalues from the input when computing the MAD, resulting in
'omitnan'— Ignore all
NaNvalues in the input. If a window contains only
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
M = movmad(A,k,'Endpoints','fill')
Endpoints — Method to treat windows near endpoints
'shrink' (default) |
'fill' | numeric or logical scalar
Method to treat windows near endpoints, specified as the comma-separated
pair consisting of
'Endpoints' and one of the following:
|Shrink the window size near the endpoints of the input to include only existing elements.|
|Do not output any MAD values when the window does not completely overlap with existing elements.|
|Replace nonexisting elements with |
|numeric or logical scalar||Replace nonexisting elements with the specified numeric or logical value.|
SamplePoints — Sample points for computing MADs
Sample points for computing MADs, specified as the comma-separated
pair consisting of
'SamplePoints' and a vector.
The sample points represent the locations of the data in
Sample points do not need to be uniformly sampled. By default, the
sample points vector is
[1 2 3 ... ].
Moving windows are defined relative to the sample points, which
must be sorted and contain unique elements. For example, if
a vector of times corresponding to the input data, then
a window that represents the time interval between
When the sample points vector has data type
then the moving window length must have type
If the sample points are nonuniformly spaced and the
pair is specified, then its value must be
Median Absolute Deviation
For a random variable vector A made up of N scalar observations, the median absolute deviation (MAD) is defined as
for i = 1,2,...,N.
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitations:
'SamplePoints' name-value pair is not
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
The maximum window length is 31 for
'SamplePoints'name-value pair is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).