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.
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.
12 % see also: MAD, RANGE, HISTO2, HISTO3, PERCENTILE, QUANTILE
15 % [1] http://www.fifi.org/doc/gnumeric-doc/html/C/gnumeric-trimmean.html
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/
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.
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.
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/>.
40 DIM = find(size(Y)>1,1);
41 if isempty(DIM), DIM = 1; end;
50 sz = [sz,ones(1,DIM-length(sz))];
53 D1 = prod(sz(1:DIM-1));
55 D3 = prod(sz(DIM+1:length(sz)));
56 Q = repmat(nan,[sz(1:DIM-1),D2,sz(DIM+1:length(sz))]);
59 xi = k + l * D1*sz(DIM) + 1 ;
61 t = Y(xi:D1:xi+D1*sz(DIM)-1);
62 t = sort(t(~isnan(t)));
66 f = sum(t(1+n:N-n))/(N-2*n);
73 %!assert(trimmean([11.4, 17.3, 21.3, 25.9, 40.1],.2),23.2)