movmad
Moving median absolute deviation
Syntax
Description
M = movmad(
returns an array of localA
,k
)k
-point平均绝对偏差(尼), where each MAD is calculated over a sliding window of lengthk
across neighboring elements ofA
.M
is the same size asA
.
Whenk
is odd, the window is centered about the element in the current position. Whenk
is 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.
If
A
is a vector, thenmovmad
operates along the length of the vectorA
.If
A
is a multidimensional array, thenmovmad
operates along the first dimension ofA
whose size does not equal 1.
M = movmad(___,
specifies the dimension ofdim
)A
to operate along for any of the previous syntaxes. For example,movmad(A,k,2)
for a matrixA
operates across the columns ofA
, computing thek
-element sliding MAD for each row.
M = movmad(___,
specifies whether to include or omitnanflag
)NaN
values inA
. For example,movmad(A,k,"omitnan")
ignoresNaN
values when computing each MAD. By default,movmad
includesNaN
values.
M = movmad(___,
specifies additional parameters for the moving MAD using one or more name-value arguments. For example, ifName,Value
)x
is a vector of time values, thenmovmad(A,k,"SamplePoints",x)
computes the moving MAD ofA
relative to the times inx
.
Examples
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×100.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×100 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 ofA
. 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×31 2 1 -1 -2 -3 -1 3 4
M = movmad(A,3,2)
M =3×30.5000 0 0.5000 0.5000 1.0000 0.5000 2.0000 1.0000 0.5000
Moving MAD Excluding Missing Values
Create a row vector containingNaN
values.
A = [4 8 NaN -1 -2 -3 NaN 3 4 5];
Compute the three-point centered moving MAD of the vector, excludingNaN
values. For windows that contain anyNaN
value,movmad
computes with the non-NaN
elements.
M = movmad(A,3,"omitnan")
M =1×102.0000 2.0000 4.5000 0.5000 1.0000 0.5000 3.0000 0.5000 1.0000 0.5000
Sample Points for Moving MAD
Compute a 3-hour centered moving MAD of the data inA
according to the time vectort
.
A = [4 8 6 -1 -2 -3]; k = hours(3); t = datetime(2016,1,1,0,0,0) + hours(0:5)
t =1x6 datetime01-Jan-2016 00:00:00 01-Jan-2016 01:00:00 01-Jan-2016 02:00:00 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×62.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×80 1 1 1 1 2 1 1
Input Arguments
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. Whenk
is a positive integer scalar, the centered MAD includes the element in the current position plus surrounding neighbors.
For example,movmad(A,3)
computes an array of local three-point MAD values.
[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. Whenkb
andkf
are positive integer scalars, the calculation is overkb+kf+1
elements. The calculation includes the element in the current position,kb
elements before the current position, andkf
elements after the current position.
For example,movmad(A,[2 1])
computes an array of local four-point MAD values.
dim
—Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.
Consider anm
-by-n
input matrix,A
:
movmad(A,k,1)
computes thek
-element sliding MADs for each column ofA
and returns anm
-by-n
matrix.movmad(A,k,2)
computes thek
-element sliding MADs for each row ofA
and returns anm
-by-n
matrix.
nanflag
—Missing value condition
"includemissing"
(default) |"includenan"
|"omitmissing"
|"omitnan"
Missing value condition, specified as one of these values:
"includemissing"
or"includenan"
— IncludeNaN
values inA
when computing each MAD. If any element in the window isNaN
, then the corresponding element inM
isNaN
."includemissing"
and"includenan"
have the same behavior."omitmissing"
or"omitnan"
— IgnoreNaN
values inA
, and compute each MAD over fewer points. If all elements in the window areNaN
, then the corresponding element inM
isNaN
."omitmissing"
and"omitnan"
have the same behavior.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and encloseName
in quotes.
Example:M = movmad(A,k,"Endpoints","fill")
Endpoints
—Method to treat windows near endpoints
"shrink"
(default) |"discard"
|"fill"
|numeric or logical scalar
Method to treat windows near endpoints, specified as one of these options:
Value | Description |
---|---|
"shrink" |
Shrink the window size near the endpoints of the input to include only existing elements. |
"discard" |
当窗口不输出任何疯狂的值呢not completely overlap with existing elements. |
"fill" |
Replace nonexisting elements withNaN . |
numeric or logical scalar | Replace nonexisting elements with the specified numeric or logical value. |
SamplePoints
—Sample points for computing MADs
vector
Sample points for computing MADs, specified as a vector. The sample points represent the locations of the data inA
. 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, ift
is a vector of times corresponding to the input data, thenmovmad(rand(1,10),3,"SamplePoints",t)
has a window that represents the time interval betweent(i)-1.5
andt(i)+1.5
.
When the sample points vector has data typedatetime
orduration
, then the moving window length must have typeduration
.
If the sample points are nonuniformly spaced andEndpoints
is specified, then its value must be"shrink"
.
More About
Median Absolute Deviation
For a finite-length vectorAmade up ofNscalar observations, the median absolute deviation (MAD) is defined as
fori = 1,2,...,N.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitations:
The
SamplePoints
name-value argument is not supported.
For more information, seeTall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
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
gpuArray
data.The
SamplePoints
name-value argument is not supported.
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Introduced in R2017aR2023a:Specify missing value condition
Include or omit missing values in the input array when computing each MAD by using the"includemissing"
or"omitmissing"
options. These options have the same behavior as the"includenan"
and"omitnan"
options, respectively.
R2023a:Improved performance when computing over matrix with sample points
Themovmad
function shows improved performance when computing over a matrix when there are sample points.
For example, this code computes the moving median absolute deviation of a 300-by-300 matrix with corresponding sample points. The code is about 1.3x faster than in the previous release.
functiontimingMovmad A = randn(300); t = sort(rand(300,1)); ticfork = 1:250 movmad(A,0.1,"SamplePoints",t);endtocend
The approximate execution times are:
R2022b:1.06 s
R2023a:0.80
The code was timed on a Windows®10, Intel®Xeon®CPU E5-1650 v4 @ 3.60 GHz test system by calling thetimingMovmad
function.
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)