]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/mad.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / mad.m
1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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}.
21 ##
22 ## The mean absolute deviation is computed as
23 ##
24 ## @example
25 ## mean (abs (@var{x} - mean (@var{x})))
26 ## @end example
27 ##
28 ## and the median absolute deviation is computed as
29 ##
30 ## @example
31 ## median (abs (@var{x} - median (@var{x})))
32 ## @end example
33 ##
34 ## Elements of @var{x} containing NaN or NA values are ignored during computations.
35 ##
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.
38 ##
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
41 ## @var{x}.
42 ##
43 ## @seealso{std}
44 ## @end deftypefn
45
46 function a = mad (X, flag = 0, dim = [])
47   ## Check input
48   if (nargin < 1)
49     print_usage ();
50   endif
51   if (nargin > 3)
52     error ("mad: too many input arguments");
53   endif
54   
55   if (!isnumeric (X))
56     error ("mad: first input must be numeric");
57   endif
58   
59   if (isempty (dim))
60     dim = min (find (size (X) > 1));
61     if (isempty(dim))
62       dim = 1;
63     endif
64   endif
65   
66   if (!isscalar (flag))
67     error ("mad: second input argument must be a scalar");
68   endif
69   if (!isscalar (dim))
70     error ("mad: dimension argument must be a scalar");
71   endif
72   
73   if (flag == 0)
74     f = @nanmean;
75   else
76     f = @nanmedian;
77   endif
78   
79   ## Compute the mad
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);
86   else
87     a = f (abs (X - f(X, dim)), dim);
88   endif
89 endfunction
90
91 ## Tests
92
93 %!assert (mad(1), 0);
94 %!test
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);