]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/trimmean.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / trimmean.m
1 function Q=trimmean(Y,p,DIM)
2 % TRIMMEAN calculates the trimmed mean by removing the fraction of p/2 upper and 
3 % p/2 lower samples. Missing values (encoded as NaN) are ignored and not taken into account. 
4 % The same number from the upper and lower values are removed, and is compatible to various
5 % spreadsheet programs including GNumeric [1], LibreOffice, OpenOffice and MS Excel.
6 %
7 %  Q = trimmean(Y,p)
8 %  Q = trimmean(Y,p,DIM)
9 %     returns the TRIMMEAN along dimension DIM of sample array Y.
10 %  If p is a vector, the TRIMMEAN for each p is computed. 
11 %
12 % see also: MAD, RANGE, HISTO2, HISTO3, PERCENTILE, QUANTILE
13 %
14 % References:
15 % [1] http://www.fifi.org/doc/gnumeric-doc/html/C/gnumeric-trimmean.html
16
17
18 %       $Id: trimmean.m 8953 2011-11-03 11:00:50Z schloegl $
19 %       Copyright (C) 2009,2010,2011 by Alois Schloegl <alois.schloegl@gmail.com>       
20 %       This function is part of the NaN-toolbox
21 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
22
23 %    This program is free software; you can redistribute it and/or modify
24 %    it under the terms of the GNU General Public License as published by
25 %    the Free Software Foundation; either version 3 of the License, or
26 %    (at your option) any later version.
27 %
28 %    This program is distributed in the hope that it will be useful,
29 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
30 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 %    GNU General Public License for more details.
32 %
33 %    You should have received a copy of the GNU General Public License
34 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
35
36 if nargin<3,
37         DIM = [];
38 end;
39 if isempty(DIM),
40         DIM = find(size(Y)>1,1);
41         if isempty(DIM), DIM = 1; end;
42 end;
43
44 if nargin<2,
45         help trimmean
46         
47 else
48         sz = size(Y);
49         if DIM > length(sz),
50                 sz = [sz,ones(1,DIM-length(sz))];
51         end;
52
53         D1 = prod(sz(1:DIM-1));
54         D2 = length(p);
55         D3 = prod(sz(DIM+1:length(sz)));
56         Q  = repmat(nan,[sz(1:DIM-1),D2,sz(DIM+1:length(sz))]);
57         for k = 0:D1-1,
58                 for l = 0:D3-1,
59                         xi = k + l * D1*sz(DIM) + 1 ;
60                         xo = k + l * D1*D2;
61                         t  = Y(xi:D1:xi+D1*sz(DIM)-1);
62                         t  = sort(t(~isnan(t)));
63                         N  = length(t); 
64                         for m=1:D2,
65                                 n  = floor(N*p(m)/2);
66                                 f  = sum(t(1+n:N-n))/(N-2*n);
67                                 Q(xo + m*D1) = f;
68                         end; 
69                 end;
70         end;
71 end;
72
73 %!assert(trimmean([11.4, 17.3, 21.3, 25.9, 40.1],.2),23.2)
74