Daniel@0: function [me, st, md, no] = nanstats(D) Daniel@0: Daniel@0: %NANSTATS Statistical operations that ignore NaNs and Infs. Daniel@0: % Daniel@0: % [mean, std, median, nans] = nanstats(D) Daniel@0: % Daniel@0: % Input and output arguments: Daniel@0: % D (struct) data or map struct Daniel@0: % (matrix) size dlen x dim Daniel@0: % Daniel@0: % me (double) columnwise mean Daniel@0: % st (double) columnwise standard deviation Daniel@0: % md (double) columnwise median Daniel@0: % no (vector) columnwise number of samples (finite, not-NaN) Daniel@0: Daniel@0: % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta juuso 300798 200900 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: (nargchk(1, 1, nargin)); % check no. of input args is correct Daniel@0: Daniel@0: if isstruct(D), Daniel@0: if strcmp(D.type,'som_map'), D = D.codebook; Daniel@0: else D = D.data; Daniel@0: end Daniel@0: end Daniel@0: [dlen dim] = size(D); Daniel@0: me = zeros(dim,1)+NaN; Daniel@0: md = me; Daniel@0: st = me; Daniel@0: no = me; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% computation Daniel@0: Daniel@0: for i = 1:dim, Daniel@0: ind = find(isfinite(D(:, i))); % indices of non-NaN/Inf elements Daniel@0: n = length(ind); % no of non-NaN/Inf elements Daniel@0: Daniel@0: me(i) = sum(D(ind, i)); % compute average Daniel@0: if n == 0, me(i) = NaN; else me(i) = me(i) / n; end Daniel@0: Daniel@0: if nargout>1, Daniel@0: md(i) = median(D(ind, i)); % compute median Daniel@0: Daniel@0: if nargout>2, Daniel@0: st(i) = sum((me(i) - D(ind, i)).^2); % compute standard deviation Daniel@0: if n == 0, st(i) = NaN; Daniel@0: elseif n == 1, st(i) = 0; Daniel@0: else st(i) = sqrt(st(i) / (n - 1)); Daniel@0: end Daniel@0: Daniel@0: if nargout>3, Daniel@0: no(i) = n; % number of samples (finite, not-NaN) Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: