nansem.m 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function y = nansem(x,dim)
  2. % FORMAT: Y = NANSEM(X,DIM)
  3. %
  4. % Standard error of the mean ignoring NaNs
  5. %
  6. % NANSTD(X,DIM) calculates the standard error of the mean along any
  7. % dimension of the N-D array X ignoring NaNs.
  8. %
  9. % If DIM is omitted NANSTD calculates the standard deviation along first
  10. % non-singleton dimension of X.
  11. %
  12. % Similar functions exist: NANMEAN, NANSTD, NANMEDIAN, NANMIN, NANMAX, and
  13. % NANSUM which are all part of the NaN-suite.
  14. % -------------------------------------------------------------------------
  15. % author: Jan Gläscher
  16. % affiliation: Neuroimage Nord, University of Hamburg, Germany
  17. % email: glaescher@uke.uni-hamburg.de
  18. %
  19. % $Revision: 1.1 $ $Date: 2004/07/22 09:02:27 $
  20. if isempty(x)
  21. y = NaN;
  22. return
  23. end
  24. if nargin < 2
  25. dim = min(find(size(x)~=1));
  26. if isempty(dim)
  27. dim = 1;
  28. end
  29. end
  30. % Find NaNs in x and nanmean(x)
  31. nans = isnan(x);
  32. count = size(x,dim) - sum(nans,dim);
  33. % Protect against a all NaNs in one dimension
  34. i = find(count==0);
  35. count(i) = 1;
  36. y = nanstd(x,dim)./sqrt(count);
  37. y(i) = i + NaN;
  38. % $Id: nansem.m,v 1.1 2004/07/22 09:02:27 glaescher Exp glaescher $