Main Content

movmad

Moving median absolute deviation

Description

M = movmad(A,k) returns the local k-point median absolute deviations (MADs), where each MAD is calculated over a sliding window of length k across neighboring elements of A. M is the same size as A.

When k is odd, the window is centered about the element in the current position. When k 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, then movmad operates along the length of the vector A.

  • If A is a multidimensional array, then movmad operates along the first dimension of A whose size does not equal 1.

  • If A is a table or timetable, then movmad operates along the variables of A. (since R2025a)

example

M = movmad(A,[kb kf]) computes the MAD with a window of length kb+kf+1 that includes the element in the current position, kb elements backward, and kf elements forward.

example

M = movmad(___,dim) specifies the dimension of A to operate along for any of the previous syntaxes. For example, movmad(A,k,2) for a matrix A operates across the columns of A, computing the k-element sliding MAD for each row.

example

M = movmad(___,nanflag) specifies whether to include or omit NaN values in A. For example, movmad(A,k,"omitnan") ignores NaN values when computing each MAD. By default, movmad includes NaN values.

example

M = movmad(___,Name,Value) specifies additional parameters for the moving MAD using one or more name-value arguments. For example, if x is a vector of time values, then movmad(A,k,"SamplePoints",x) computes the moving MAD of A relative to the times in x.

example

Examples

collapse all

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

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

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

Create a row vector containing NaN values.

A = [4 8 NaN -1 -2 -3 NaN 3 4 5];

Compute the three-point centered moving MAD of the vector, excluding NaN values. For windows that contain any NaN value, movmad computes with the non-NaN elements.

M = movmad(A,3,"omitnan")
M = 1×10

    2.0000    2.0000    4.5000    0.5000    1.0000    0.5000    3.0000    0.5000    1.0000    0.5000

Compute a 3-hour centered moving MAD of the data in A according to the time vector t.

A = [4 8 6 -1 -2 -3];
k = hours(3);
t = datetime(2016,1,1,0,0,0) + hours(0:5)
t = 1×6 datetime
   01-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×6

    2.0000    2.0000    2.0000    1.0000    1.0000    0.5000

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

Create a table from the columns of a magic square.

A = magic(3);
T = array2table(A)
T=3×3 table
    A1    A2    A3
    __    __    __

    8     1     6 
    3     5     7 
    4     9     2 

Calculate the moving MADs of the table variables using movmad.

M = movmad(T,3)
M=3×3 table
    A1     A2    A3 
    ___    __    ___

    2.5    2     0.5
      1    4       1
    0.5    2     2.5

To calculate moving MADs only for specified variables, specify the DataVariables name-value argument.

M2 = movmad(T,3,DataVariables=["A2" "A3"])
M2=3×3 table
    A1    A2    A3 
    __    __    ___

    8     2     0.5
    3     4       1
    4     2     2.5

To append the moving MADs to the table instead of replacing values, specify ReplaceValues as false.

M3 = movmad(T,3,ReplaceValues=false)
M3=3×6 table
    A1    A2    A3    A1_movmad    A2_movmad    A3_movmad
    __    __    __    _________    _________    _________

    8     1     6        2.5           2           0.5   
    3     5     7          1           4             1   
    4     9     2        0.5           2           2.5   

Input Arguments

collapse all

Input data, specified as a vector, matrix, multidimensional array, table, or timetable.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | table | timetable

Window length, specified as a numeric or duration scalar. When k 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.

movmad(A,3) computation. The elements in the sample window are 1, 3, and 2, so the resulting local MAD is 1.

Directional window length, specified as a numeric or duration row vector containing two elements. When kb and kf are positive integer scalars, the calculation is over kb+kf+1 elements. The calculation includes the element in the current position, kb elements before the current position, and kf elements after the current position.

For example, movmad(A,[2 1]) computes an array of local four-point MAD values.

movmad(A,[2 1]) computation. The elements in the sample window are 4, 1, 3, and 2, so the resulting local MAD is 1.

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 whose size does not equal 1.

Consider an m-by-n input matrix, A:

  • movmad(A,k,1) computes the k-element sliding MADs for each column of A and returns an m-by-n matrix.

    movmad(A,k,1) column-wise operation

  • movmad(A,k,2) computes the k-element sliding MADs for each row of A and returns an m-by-n matrix.

    movmad(A,k,2) row-wise operation

If A is a table or timetable, then you cannot specify dim. The movmad function always operates along the variables of tables and timetables. (since R2025a)

Missing value condition, specified as one of these values:

  • "includemissing" or "includenan" — Include NaN values in A when computing each MAD. If any element in the window is NaN, then the corresponding element in M is NaN. "includemissing" and "includenan" have the same behavior.

  • "omitmissing" or "omitnan" — Ignore NaN values in A, and compute each MAD over fewer points. If all elements in the window are NaN, then the corresponding element in M is NaN. "omitmissing" and "omitnan" have the same behavior.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value 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 enclose Name in quotes.

Example: M = movmad(A,k,"Endpoints","fill")

Method to treat windows near endpoints, specified as one of these options:

ValueDescription
"shrink"Shrink the window size near the endpoints of the input to include only existing elements.
"discard"

Do not output any MAD values when the window does not completely overlap with existing elements.

If A is a table or timetable, then you cannot specify Endpoints as "discard".

"fill"Replace nonexisting elements with NaN.
numeric or logical scalarReplace nonexisting elements with the specified numeric or logical value.

Sample points for computing MADs, specified as a vector. The sample points represent the locations of the data in A. 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 t is a vector of times corresponding to the input data, then movmad(rand(1,10),3,"SamplePoints",t) has a window that represents the time interval between t(i)-1.5 and t(i)+1.5.

When the sample points vector has data type datetime or duration, then the moving window length must have type duration.

If the sample points are nonuniformly spaced and Endpoints is specified, then its value must be "shrink".

Since R2025a

Table or timetable variables to operate on, specified as one of the options in this table.

  • If you do not specify DataVariables, then movmad operates on all variables. This behavior is the default behavior.

  • If you specify DataVariables, then movmad operates only on the specified variables. Other variables not specified by DataVariables pass through to the output without modification.

Indexing SchemeValues to SpecifyExamples

Variable name

  • A string scalar or character vector

  • A string array or cell array of character vectors

  • A pattern object

  • "A" or 'A' — A variable named A

  • ["A" "B"] or {'A','B'} — Two variables named A and B

  • "Var"+digitsPattern(1) — Variables named "Var" followed by a single digit

Variable index

  • An index number that refers to the location of a variable in the table

  • A vector of numbers

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 (false) values.

  • 3 — The third variable from the table

  • [2 3] — The second and third variables from the table

  • [false false true] — The third variable

Function handle

  • A function handle that takes a table variable as input and returns a logical scalar

  • @isnumeric — All the variables containing numeric values

Variable type

  • A vartype subscript that selects variables of a specified type

  • vartype("numeric") — All the variables containing numeric values

Since R2025a

Replace values indicator, specified as one of these values when A is a table or timetable.

  • true or 1 — In the output table or timetable, replace input table or timetable variables with variables containing output values from movmad.

  • false or 0 — Append variables containing output values from movmad to the output table or timetable.

For vector, matrix, or multidimensional array input data, ReplaceValues is not supported.

More About

collapse all

Extended Capabilities

expand all

Version History

Introduced in R2017a

expand all

See Also

Functions