annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/nanstats.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [me, st, md, no] = nanstats(D)
wolffd@0 2
wolffd@0 3 %NANSTATS Statistical operations that ignore NaNs and Infs.
wolffd@0 4 %
wolffd@0 5 % [mean, std, median, nans] = nanstats(D)
wolffd@0 6 %
wolffd@0 7 % Input and output arguments:
wolffd@0 8 % D (struct) data or map struct
wolffd@0 9 % (matrix) size dlen x dim
wolffd@0 10 %
wolffd@0 11 % me (double) columnwise mean
wolffd@0 12 % st (double) columnwise standard deviation
wolffd@0 13 % md (double) columnwise median
wolffd@0 14 % no (vector) columnwise number of samples (finite, not-NaN)
wolffd@0 15
wolffd@0 16 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto
wolffd@0 17 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 18
wolffd@0 19 % Version 2.0beta juuso 300798 200900
wolffd@0 20
wolffd@0 21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 22 %% check arguments
wolffd@0 23
wolffd@0 24 (nargchk(1, 1, nargin)); % check no. of input args is correct
wolffd@0 25
wolffd@0 26 if isstruct(D),
wolffd@0 27 if strcmp(D.type,'som_map'), D = D.codebook;
wolffd@0 28 else D = D.data;
wolffd@0 29 end
wolffd@0 30 end
wolffd@0 31 [dlen dim] = size(D);
wolffd@0 32 me = zeros(dim,1)+NaN;
wolffd@0 33 md = me;
wolffd@0 34 st = me;
wolffd@0 35 no = me;
wolffd@0 36
wolffd@0 37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 38 %% computation
wolffd@0 39
wolffd@0 40 for i = 1:dim,
wolffd@0 41 ind = find(isfinite(D(:, i))); % indices of non-NaN/Inf elements
wolffd@0 42 n = length(ind); % no of non-NaN/Inf elements
wolffd@0 43
wolffd@0 44 me(i) = sum(D(ind, i)); % compute average
wolffd@0 45 if n == 0, me(i) = NaN; else me(i) = me(i) / n; end
wolffd@0 46
wolffd@0 47 if nargout>1,
wolffd@0 48 md(i) = median(D(ind, i)); % compute median
wolffd@0 49
wolffd@0 50 if nargout>2,
wolffd@0 51 st(i) = sum((me(i) - D(ind, i)).^2); % compute standard deviation
wolffd@0 52 if n == 0, st(i) = NaN;
wolffd@0 53 elseif n == 1, st(i) = 0;
wolffd@0 54 else st(i) = sqrt(st(i) / (n - 1));
wolffd@0 55 end
wolffd@0 56
wolffd@0 57 if nargout>3,
wolffd@0 58 no(i) = n; % number of samples (finite, not-NaN)
wolffd@0 59 end
wolffd@0 60 end
wolffd@0 61 end
wolffd@0 62 end
wolffd@0 63
wolffd@0 64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 65