]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/trimmean.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / trimmean.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} {@var{a} =} trimmean (@var{x}, @var{p})
18 ##
19 ## Compute the trimmed mean.
20 ##
21 ## The trimmed mean of @var{x} is defined as the mean of @var{x} excluding the
22 ## highest and lowest @var{p} percent of the data.
23 ##
24 ## For example
25 ##
26 ## @example
27 ## mean ([-inf, 1:9, inf])
28 ## @end example
29 ##
30 ## is NaN, while
31 ##
32 ## @example
33 ## trimmean ([-inf, 1:9, inf], 10)
34 ## @end example
35 ##
36 ## excludes the infinite values, which make the result 5.
37 ##
38 ## @seealso{mean}
39 ## @end deftypefn
40
41 function a = trimmean(x, p, varargin)
42   if (nargin != 2 && nargin != 3)
43     print_usage;
44   endif
45   y = sort(x, varargin{:});
46   sz = size(x);
47   if nargin < 3
48     dim = min(find(sz>1));
49     if isempty(dim), dim=1; endif;
50   else
51     dim = varargin{1};
52   endif
53   idx = cell (0);
54   for i=1:length(sz), idx{i} = 1:sz(i); end;
55   trim = round(sz(dim)*p*0.01);
56   idx{dim} = 1+trim : sz(dim)-trim;
57   a = mean (y (idx{:}), varargin{:});
58 endfunction