zeromean.m 724 B

12345678910111213141516171819202122232425262728
  1. function f = zeromean(m,dim)
  2. % function f = zeromean(m,dim)
  3. %
  4. % <m> is a matrix
  5. % <dim> (optional) is the dimension of interest.
  6. % if supplied, subtract off the mean of each case oriented along <dim>.
  7. % if [] or not supplied, subtract off the global mean.
  8. %
  9. % subtract off the mean of <m>, either of individual cases or the global mean.
  10. % we use nanmean to deal with NaNs gracefully.
  11. %
  12. % note some weird cases:
  13. % zeromean([]) is []
  14. % zeromean([NaN NaN]) is [NaN NaN]
  15. %
  16. % example:
  17. % a = [1 1; 2 0];
  18. % isequal(zeromean(a),[0 0; 1 -1])
  19. % a = [1 NaN];
  20. % isequalwithequalnans(zeromean(a,1),[0 NaN])
  21. % do it
  22. if ~exist('dim','var') || isempty(dim)
  23. f = m - nanmean(m(:));
  24. else
  25. f = bsxfun(@minus,m,nanmean(m,dim));
  26. end