FORMAT: Y = NANSTD(X,DIM,FLAG) Standard deviation ignoring NaNs This function enhances the functionality of NANSTD as distributed in the MATLAB Statistics Toolbox and is meant as a replacement (hence the identical name). NANSTD(X,DIM) calculates the standard deviation along any dimension of the N-D array X ignoring NaNs. NANSTD(X,DIM,0) normalizes by (N-1) where N is SIZE(X,DIM). This make NANSTD(X,DIM).^2 the best unbiased estimate of the variance if X is a sample of a normal distribution. If omitted FLAG is set to zero. NANSTD(X,DIM,1) normalizes by N and produces the square root of the second moment of the sample about the mean. If DIM is omitted NANSTD calculates the standard deviation along first non-singleton dimension of X. Similar replacements exist for NANMEAN, NANMEDIAN, NANMIN, NANMAX, and NANSUM which are all part of the NaN-suite. See also STD
0001 function y = nanstd2(x,dim,flag) 0002 % FORMAT: Y = NANSTD(X,DIM,FLAG) 0003 % 0004 % Standard deviation ignoring NaNs 0005 % 0006 % This function enhances the functionality of NANSTD as distributed in 0007 % the MATLAB Statistics Toolbox and is meant as a replacement (hence the 0008 % identical name). 0009 % 0010 % NANSTD(X,DIM) calculates the standard deviation along any dimension of 0011 % the N-D array X ignoring NaNs. 0012 % 0013 % NANSTD(X,DIM,0) normalizes by (N-1) where N is SIZE(X,DIM). This make 0014 % NANSTD(X,DIM).^2 the best unbiased estimate of the variance if X is 0015 % a sample of a normal distribution. If omitted FLAG is set to zero. 0016 % 0017 % NANSTD(X,DIM,1) normalizes by N and produces the square root of the 0018 % second moment of the sample about the mean. 0019 % 0020 % If DIM is omitted NANSTD calculates the standard deviation along first 0021 % non-singleton dimension of X. 0022 % 0023 % Similar replacements exist for NANMEAN, NANMEDIAN, NANMIN, NANMAX, and 0024 % NANSUM which are all part of the NaN-suite. 0025 % 0026 % See also STD 0027 0028 % ------------------------------------------------------------------------- 0029 % author: Jan Gl�scher 0030 % affiliation: Neuroimage Nord, University of Hamburg, Germany 0031 % email: glaescher@uke.uni-hamburg.de 0032 % 0033 % $Revision: 1.1 $ $Date: 2014/06/10 20:18:11 $ 0034 0035 if isempty(x) 0036 y = NaN; 0037 return 0038 end 0039 0040 if nargin < 3 0041 flag = 0; 0042 end 0043 0044 if nargin < 2 0045 dim = min(find(size(x)~=1)); 0046 if isempty(dim) 0047 dim = 1; 0048 end 0049 end 0050 0051 0052 % Find NaNs in x and nanmean(x) 0053 nans = isnan(x); 0054 avg = nanmean(x,dim); 0055 0056 % create array indicating number of element 0057 % of x in dimension DIM (needed for subtraction of mean) 0058 tile = ones(1,max(ndims(x),dim)); 0059 tile(dim) = size(x,dim); 0060 0061 % remove mean 0062 x = x - repmat(avg,tile); 0063 0064 count = size(x,dim) - sum(nans,dim); 0065 0066 % Replace NaNs with zeros. 0067 x(isnan(x)) = 0; 0068 0069 0070 % Protect against a all NaNs in one dimension 0071 i = find(count==0); 0072 0073 if flag == 0 0074 y = sqrt(sum(x.*x,dim)./max(count-1,1)); 0075 else 0076 y = sqrt(sum(x.*x,dim)./max(count,1)); 0077 end 0078 y(i) = i + NaN; 0079 0080 % $Id: nanstd2.m,v 1.1 2014/06/10 20:18:11 cbassuser Exp $