X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ftsa-4.2.4%2Fsumskipnan.m;fp=octave_packages%2Ftsa-4.2.4%2Fsumskipnan.m;h=6103269a6e33dce76f8f61f5bfc657dd5420d9dd;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/tsa-4.2.4/sumskipnan.m b/octave_packages/tsa-4.2.4/sumskipnan.m new file mode 100644 index 0000000..6103269 --- /dev/null +++ b/octave_packages/tsa-4.2.4/sumskipnan.m @@ -0,0 +1,193 @@ +function [o,count,SSQ] = sumskipnan(x, DIM, W) +% SUMSKIPNAN adds all non-NaN values. +% +% All NaN's are skipped; NaN's are considered as missing values. +% SUMSKIPNAN of NaN's only gives O; and the number of valid elements is return. +% SUMSKIPNAN is also the elementary function for calculating +% various statistics (e.g. MEAN, STD, VAR, RMS, MEANSQ, SKEWNESS, +% KURTOSIS, MOMENT, STATISTIC etc.) from data with missing values. +% SUMSKIPNAN implements the DIMENSION-argument for data with missing values. +% Also the second output argument return the number of valid elements (not NaNs) +% +% Y = sumskipnan(x [,DIM]) +% [Y,N,SSQ] = sumskipnan(x [,DIM]) +% [...] = sumskipnan(x, DIM, W) +% +% x input data +% DIM dimension (default: []) +% empty DIM sets DIM to first non singleton dimension +% W weight vector for weighted sum, numel(W) must fit size(x,DIM) +% Y resulting sum +% N number of valid (not missing) elements +% SSQ sum of squares +% +% the function FLAG_NANS_OCCURED() returns whether any value in x +% is a not-a-number (NaN) +% +% features: +% - can deal with NaN's (missing values) +% - implements dimension argument. +% - computes weighted sum +% - compatible with Matlab and Octave +% +% see also: FLAG_NANS_OCCURED, SUM, NANSUM, MEAN, STD, VAR, RMS, MEANSQ, +% SSQ, MOMENT, SKEWNESS, KURTOSIS, SEM + + +% 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 3 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: sumskipnan.m 9033 2011-11-08 20:58:07Z schloegl $ +% Copyright (C) 2000-2005,2009,2011 by Alois Schloegl +% This function is part of the NaN-toolbox +% http://pub.ist.ac.at/~schloegl/matlab/NaN/ + + +global FLAG_NANS_OCCURED; + +if nargin<2, + DIM = []; +end; +if nargin<3, + W = []; +end; + +% an efficient implementation in C of the following lines +% could significantly increase performance +% only one loop and only one check for isnan is needed +% An MEX-Implementation is available in sumskipnan.cpp +% +% Outline of the algorithm: +% for { k=1,o=0,count=0; k++; k1,1); + if isempty(DIM), DIM = 1; end; +end +if (DIM<1), DIM = 1; end; %% Hack, because min([])=0 for FreeMat v3.5 + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% non-float data +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +if (isempty(W) && (~(isa(x,'float') || isa(x,'double')))) || ~flag_implicit_skip_nan(), %%% skip always NaN's + if ~isempty(W) + error('SUMSKIPNAN: weighted sum of integers not supported, yet'); + end; + x = double(x); + o = sum(x,DIM); + if nargout>1 + sz = size(x); + N = sz(DIM); + sz(DIM) = 1; + count = repmat(N,sz); + if nargout>2 + x = x.*x; + SSQ = sum(x,DIM); + end; + end; + return; +end; + +if (length(size(x))=3), + [o,count,SSQ] = sumskipnan_mex(real(x),DIM,FLAG_NANS_OCCURED,W); + if (~isreal(x)) + [io,icount,iSSQ] = sumskipnan_mex(imag(x),DIM,FLAG_NANS_OCCURED,W); + if any(count(:)-icount(:)) + error('Number of NaNs differ for REAL and IMAG part'); + else + o = o+i*io; + SSQ = SSQ+iSSQ; + end; + end; + return; + end; +end; + +if ~isempty(W) + error('weighted sumskipnan requires sumskipnan_mex'); +end; + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% count non-NaN's +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +if nargout>1, + count = sum(x==x,DIM); + FLAG_NANS_OCCURED = any(count(:)2, + x = real(x).^2 + imag(x).^2; + SSQ = sum(x,DIM); +end; + +%!assert(sumskipnan([1,2],1),[1,2]) +%!assert(sumskipnan([1,NaN],2),1) +%!assert(sumskipnan([1,NaN],2),1) +%!assert(sumskipnan([nan,1,4,5]),10) +%!assert(sumskipnan([nan,1,4,5]',1,[3;2;1;0]),6) + + +