Main Content

mad

Mean or median absolute deviation

Description

example

y= mad(X)returns the mean absolute deviation of the values inX.

  • IfXis a vector, thenmadreturns the mean or median absolute deviation of the values inX.

  • IfXis a matrix, thenmadreturns a row vector containing the mean or median absolute deviation of each column ofX.

  • IfXis a multidimensional array, thenmadoperates along the first nonsingleton dimension ofX.

example

y= mad(X,flag)specifies whether to compute the mean absolute deviation (flag = 0, the default) or the median absolute deviation (flag = 1).

example

y= mad(X,flag, '所有')returns the mean or median absolute deviation of all elements ofX.

example

y= mad(X,flag,dim)returns the mean or median absolute deviation along the operating dimensiondimofX.

example

y= mad(X,flag,vecdim)returns the mean or median absolute deviation over the dimensions specified in the vectorvecdim. For example, ifXis a 2-by-3-by-4 array, thenmad(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 ofX.

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 setxof normally distributed data. Create another data setxothat contains the elements ofxand an additional outlier.

rng('default')% For reproducibilityx = 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)/mad(x)
r2 = 1.1833

计算平均绝对偏差的比值s 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 compared to 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 arrayXand 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 inX.

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

meandevis the mean absolute deviation of all the elements inX, andmediandevis the median absolute deviation of all the elements inX.

Find the median absolute deviation along different dimensions for a multidimensional array.

Set the random seed for reproducibility of the results.

rng('default')

Create a 1-by-3-by-2 array of random numbers.

X = randn([1,3,2])
X = X(:,:,1) = 0.5377 1.8339 -2.2588 X(:,:,2) = 0.8622 0.3188 -1.3077

Find the median absolute deviation ofXalong the default dimension.

Y2 = mad(X,1)% Flag is set to 1 for the median absolute deviation
Y2 = Y2(:,:,1) = 1.2962 Y2(:,:,2) = 0.5434

By default,madoperates along the first dimension ofXwhose size does not equal 1. In this case, this dimension is the second dimension ofX. Therefore,Y2is a 1-by-1-by-2 array.

Find the median absolute deviation ofXalong the third dimension.

Y3 = mad(X,1,3)
Y3 =1×30.1623 0.7576 0.4756

Y3is a 1-by-3 matrix.

Find the mean absolute deviation over multiple dimensions by using thevecdiminput argument.

Set the random seed for reproducibility of the results.

rng('default')

Create a 4-by-3-by-2 array of random numbers.

X = randn([4 3 2])
X = X(:,:,1) = 0.5377 0.3188 3.5784 1.8339 -1.3077 2.7694 -2.2588 -0.4336 -1.3499 0.8622 0.3426 3.0349 X(:,:,2) = 0.7254 -0.1241 0.6715 -0.0631 1.4897 -1.2075 0.7147 1.4090 0.7172 -0.2050 1.4172 1.6302

Find the mean absolute deviation of each page ofXby specifying the first and second dimensions.

ypage = mad(X,0,[1 2])
ypage = ypage(:,:,1) = 1.4626 ypage(:,:,2) = 0.6652

For example,ypage(:,:,2)is the mean absolute deviation of all the elements inX(:,:,2), and is equivalent to specifyingmad(X(:,:,2),0,'all').

Find the mean absolute deviation of the elements in eachX(:,i,:)slice by specifying the first and third dimensions.

ycol = mad(X,0,[1 3])
ycol =1×30.8330 0.7872 1.5227

For example,ycol(3)is the mean absolute deviation of all the elements inX(:,3,:), and is equivalent to specifyingmad(X(:,3,:),0,'all').

Input Arguments

collapse all

Input data that represents a sample from a population, specified as a vector, matrix, or multidimensional array.

  • IfXis a vector, thenmadreturns the mean or median absolute deviation of the values inX.

  • IfXis a matrix, thenmadreturns a row vector containing the mean or median absolute deviation of each column ofX.

  • IfXis a multidimensional array, thenmadoperates along the first nonsingleton dimension ofX.

To specify the operating dimension whenXis a matrix or an array, use thediminput argument.

madtreatsNaNsas missing values and removes them.

Data Types:single|double

Indicator for the type of deviation, specified as0or1.

  • Ifflagis0(default), thenmadcomputes the mean absolute deviation,mean(abs(X – mean(X))).

  • Ifflagis1, thenmadcomputes the median absolute deviation,median(abs(X – median(X))).

Data Types:single|double|logical

Dimension along which to operate, specified as a positive integer. If you do not specify a value fordim, then the default is the first dimension ofXwhose size does not equal 1.

Consider the mean absolute deviation of a matrixX:

  • Ifdimis equal to 1, thenmad(X)returns a row vector that contains the mean absolute deviation of each column inX.

  • Ifdimis equal to 2, thenmad(X)returns a column vector that contains the mean absolute deviation of each row inX.

Data Types:single|double

Vector of dimensions, specified as a positive integer vector. Each element ofvecdimrepresents a dimension of the input arrayX. The outputyhas length 1 in the specified operating dimensions. The other dimension lengths are the same forXandy.

For example, ifXis a 2-by-3-by-3 array, thenmad(X,0,[1 2])returns a 1-by-1-by-3 array. Each element of the output array is the mean absolute deviation of the elements on the corresponding page ofX.

Data Types:single|double

Output Arguments

collapse all

Mean or median absolute deviation, returned as a scalar, vector, matrix, or multidimensional array. Ifflag0(默认),thenyis the mean absolute deviation of the values inX,mean(abs(X – mean(X))). Ifflagis 1, thenyis the median absolute deviation of the values inX,median(abs(X – median(X))).

Tips

  • For normally distributed data, multiplymadby 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。,j .图基。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