nanstats.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 66 行

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?