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

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