1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} mad (@var{x})
18 ## @deftypefnx{Function File} mad (@var{x}, @var{flag})
19 ## @deftypefnx{Function File} mad (@var{x}, @var{flag}, @var{dim})
20 ## Compute the mean/median absolute deviation of @var{x}.
22 ## The mean absolute deviation is computed as
25 ## mean (abs (@var{x} - mean (@var{x})))
28 ## and the median absolute deviation is computed as
31 ## median (abs (@var{x} - median (@var{x})))
34 ## Elements of @var{x} containing NaN or NA values are ignored during computations.
36 ## If @var{flag} is 0, the absolute mean deviation is computed, and if @var{flag}
37 ## is 1, the absolute median deviation is computed. By default @var{flag} is 0.
39 ## This is done along the dimension @var{dim} of @var{x}. If this variable is not
40 ## given, the mean/median absolute deviation s computed along the smallest dimension of
46 function a = mad (X, flag = 0, dim = [])
52 error ("mad: too many input arguments");
56 error ("mad: first input must be numeric");
60 dim = min (find (size (X) > 1));
67 error ("mad: second input argument must be a scalar");
70 error ("mad: dimension argument must be a scalar");
80 if (prod(size(X)) != size(X,dim))
81 sz = ones (1, length (size (X)));
82 sz (dim) = size (X,dim);
83 a = f (abs (X - repmat (f (X, dim), sz)), dim);
84 elseif (all (size (X) > 1))
85 a = f (abs (X - ones (size(X, 1), 1) * f (X, dim)), dim);
87 a = f (abs (X - f(X, dim)), dim);
95 %! X = eye(3); abs_mean = [4/9, 4/9, 4/9]; abs_median=[0,0,0];
96 %! assert(mad(X), abs_mean, eps);
97 %! assert(mad(X, 0), abs_mean, eps);
98 %! assert(mad(X,1), abs_median);