This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

mad

Mean or median absolute deviation

Syntax

y = mad(X)
y = mad(X,1)
y = mad(X,0)
y = mad(X,flag,'all')
y = mad(X,flag,dim)
y = mad(X,flag,vecdim)

Description

y = mad(X) returns the mean absolute deviation of the values in X. For vector input, y is mean(abs(X-mean(X))). For a matrix input, y is a row vector containing the mean absolute deviation of each column of X. For N-dimensional arrays, mad operates along the first nonsingleton dimension of X.

example

y = mad(X,1) returns the median absolute deviation of the values in X. For vector input, y is median(abs(X-median(X))). For a matrix input, y is a row vector containing the median absolute deviation of each column of X. For N-dimensional arrays, mad operates along the first nonsingleton dimension of X.

example

y = mad(X,0) is the same as mad(X), and returns the mean absolute deviation of the values in X.

example

y = mad(X,flag,'all') returns the absolute deviation of all elements of X. The flag value is 0 or 1 to indicate mean or median absolute deviation, respectively.

y = mad(X,flag,dim) computes the absolute deviation along the operating dimension dim of X.

y = mad(X,flag,vecdim) returns the absolute deviation over the dimensions specified in the vector vecdim. Each element of vecdim represents a dimension of the input array X. The output y has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and y. For example, if X is a 2-by-3-by-4 array, then mad(X,0,[1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the mean absolute deviation of the elements on the corresponding page of X.

Examples

collapse all

Compare the robustness of the standard deviation, mean absolute deviation, and median absolute deviation in the presence of outliers.

Create a data set x of normally distributed data. Create another data set xo that contains the elements of x and an additional outlier.

rng('default') % For reproducibility
x = normrnd(0,1,1,50);
xo = [x 10];

Compute the ratio of the standard deviations of the two data sets.

r1 = std(xo)/std(x)
r1 = 1.4633

Compute the ratio of the mean absolute deviations of the two data sets.

r2 = mad(xo,0)/mad(x,0)
r2 = 1.1833

Compute the ratio of the median absolute deviations of the two data sets.

r3 = mad(xo,1)/mad(x,1)
r3 = 1.0336

In this case, the median absolute deviation is less influenced by the outlier than the other two scale estimates.

Find the mean and median absolute deviations of all the values in an array.

Create a 3-by-5-by-2 array X and add an outlier.

X = reshape(1:30,[3 5 2]);
X(6) = 100
X = 
X(:,:,1) =

     1     4     7    10    13
     2     5     8    11    14
     3   100     9    12    15


X(:,:,2) =

    16    19    22    25    28
    17    20    23    26    29
    18    21    24    27    30

Find the mean and median absolute deviations of the elements in X.

meandev = mad(X,0,'all')
meandev = 10.1178
mediandev = mad(X,1,'all')
mediandev = 7.5000

meandev is the mean absolute deviation of all the elements in X, and mediandev is the median absolute deviation of all the elements in X.

Tips

  • mad treats NaNs as missing values and removes them.

  • For normally distributed data, multiply mad by one of the following factors to obtain an estimate of the normal scale parameter σ:

    • sigma = 1.253*mad(X,0) — For mean absolute deviation

    • sigma = 1.4826*mad(X,1) — For median absolute deviation

References

[1] Mosteller, F., and J. Tukey. Data Analysis and Regression. Upper Saddle River, NJ: Addison-Wesley, 1977.

[2] Sachs, L. Applied Statistics: A Handbook of Techniques. New York: Springer-Verlag, 1984, p. 253.

Extended Capabilities

See Also

| |

Introduced before R2006a