### Function Reference: `mad`

: m = mad (x)
: m = mad (x, opt)
: m = mad (x, opt, dim)
: m = mad (x, opt, vecdim)
: m = mad (x, opt, "all")

Compute the mean or median absolute deviation (MAD) of the elements of x.

The mean absolute deviation is defined as

     mad = mean (abs (x - mean (x)))

The median absolute deviation is defined as

     mad = median (abs (x - median (x)))

If x is a vector, compute `mad` for each element in x. If x is an array the calculation is performed over the first non-singleton dimension.

`mad` excludes NaN values from calculation similar to using the `omitnan` option in `var`, `mean`, and `median`.

The optional argument opt determines whether mean or median absolute deviation is calculated. The default is 0 which corresponds to mean absolute deviation; a value of 1 corresponds to median absolute deviation. Passing an empty input [] defaults to mean absolute deviation (opt = 0).

The optional argument dim forces `mad` to operate along the specified dimension. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will result in an output of 0.

Specifying the dimension as vecdim, a vector of non-repeating dimensions, will return the `mad` over the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension included in vecdim greater than `ndims (x)` is ignored.

Specifying the dimension as `"all"` will force `mad` to operate on all elements of x, and is equivalent to `mad (x(:))`.

As a measure of dispersion, `mad` is less affected by outliers than `std`. See also: bounds, range, iqr, std, mean, median

Source Code: mad