X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=octave_packages%2Fnan-2.5.5%2Fmedian.m;fp=octave_packages%2Fnan-2.5.5%2Fmedian.m;h=bcaa742a2094c6743a70f2ba5a72f82e6414042d;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hp=0000000000000000000000000000000000000000;hpb=1705066eceaaea976f010f669ce8e972f3734b05;p=CreaPhase.git diff --git a/octave_packages/nan-2.5.5/median.m b/octave_packages/nan-2.5.5/median.m new file mode 100644 index 0000000..bcaa742 --- /dev/null +++ b/octave_packages/nan-2.5.5/median.m @@ -0,0 +1,94 @@ +function [y]=median(x,DIM) +% MEDIAN data elements, +% [y]=median(x [,DIM]) +% +% DIM dimension +% 1: median of columns +% 2: median of rows +% N: median of N-th dimension +% default or []: first DIMENSION, with more than 1 element +% +% features: +% - can deal with NaN's (missing values) +% - accepts dimension argument like in Matlab in Octave, too. +% - compatible to Matlab and Octave +% +% see also: SUMSKIPNAN + +% This program is free software; you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation; either version 2 of the License, or +% (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program; If not, see . + +% $Id: median.m 9033 2011-11-08 20:58:07Z schloegl $ +% Copyright (C) 2000-2003,2009 by Alois Schloegl +% This function is part of the NaN-toolbox +% http://pub.ist.ac.at/~schloegl/matlab/NaN/ + +global FLAG_NANS_OCCURED; + +% check dimension of x +sz=size(x); + +% find the dimension for median +if nargin<2, + DIM=min(find(sz>1)); + if isempty(DIM), DIM=1; end; +end; + +if DIM>length(sz), + sz = [sz,ones(1,DIM-length(sz))]; +end; + +D1 = prod(sz(1:DIM-1)); +D2 = sz(DIM); +D3 = prod(sz(DIM+1:length(sz))); +D0 = [sz(1:DIM-1),1,sz(DIM+1:length(sz))]; +y = repmat(nan,D0); +flag_MexKthElement = exist('kth_element','file')==3; + +for k = 0:D1-1, +for l = 0:D3-1, + xi = k + l * D1*sz(DIM) + 1 ; + xo = k + l * D1 + 1; + t = x(xi+(0:sz(DIM)-1)*D1); + t = t(~isnan(t)); + n = length(t); + + if n==0, + y(xo) = nan; + elseif flag_MexKthElement, + if (D1==1) t = t+0.0; end; % make sure a real copy (not just a reference to x) is used + flag_KthE = 0; % fast kth_element can be used, because t does not contain any NaN and there is need to care about in-place sorting + if ~rem(n,2), + y(xo) = sum( kth_element( double(t), n/2 + [0,1], flag_KthE) ) / 2; + elseif rem(n,2), + y(xo) = kth_element(double(t), (n+1)/2, flag_KthE); + end; + else + t = sort(t); + if ~rem(n,2), + y(xo) = (t(n/2) + t(n/2+1)) / 2; + elseif rem(n,2), + y(xo) = t((n+1)/2); + end; + end + + if (n