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} {@var{a} =} trimmean (@var{x}, @var{p})
19 ## Compute the trimmed mean.
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.
27 ## mean ([-inf, 1:9, inf])
33 ## trimmean ([-inf, 1:9, inf], 10)
36 ## excludes the infinite values, which make the result 5.
41 function a = trimmean(x, p, varargin)
42 if (nargin != 2 && nargin != 3)
45 y = sort(x, varargin{:});
48 dim = min(find(sz>1));
49 if isempty(dim), dim=1; endif;
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{:});