/branches/schloegl/octave-forge/extra/NaN/meandev.m

# · MATLAB · 80 lines · 25 code · 11 blank · 44 comment · 6 complexity · 5186757863128c60c1134fc291cecd15 MD5 · raw file

  1. function R = meandev(i,DIM)
  2. % MEANDEV estimates the Mean deviation
  3. % (note that according to [1,2] this is the mean deviation;
  4. % not the mean absolute deviation)
  5. %
  6. % y = meandev(x,DIM)
  7. % calculates the mean deviation of x in dimension DIM
  8. %
  9. % DIM dimension
  10. % 1: STATS of columns
  11. % 2: STATS of rows
  12. % default or []: first DIMENSION, with more than 1 element
  13. %
  14. % features:
  15. % - can deal with NaN's (missing values)
  16. % - dimension argument
  17. % - compatible to Matlab and Octave
  18. % - global FLAG_implicit_skip_nan
  19. %
  20. % see also: SUMSKIPNAN, VAR, STD, MAD
  21. %
  22. % REFERENCE(S):
  23. % [1] http://mathworld.wolfram.com/MeanDeviation.html
  24. % [2] L. Sachs, "Applied Statistics: A Handbook of Techniques", Springer-Verlag, 1984, page 253.
  25. % [3] http://mathworld.wolfram.com/MeanAbsoluteDeviation.html
  26. % [4] Kenney, J. F. and Keeping, E. S. "Mean Absolute Deviation." §6.4 in Mathematics of Statistics, Pt. 1, 3rd ed. Princeton, NJ: Van Nostrand, pp. 76-77 1962.
  27. % This program is free software; you can redistribute it and/or modify
  28. % it under the terms of the GNU General Public License as published by
  29. % the Free Software Foundation; either version 2 of the License, or
  30. % (at your option) any later version.
  31. %
  32. % This program is distributed in the hope that it will be useful,
  33. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. % GNU General Public License for more details.
  36. %
  37. % You should have received a copy of the GNU General Public License
  38. % along with this program; if not, write to the Free Software
  39. % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. % Version 1.16
  41. % 12 Mar 2002
  42. % Copyright (c) 2000-2002 by Alois Schloegl
  43. % a.schloegl@ieee.org
  44. % check input arguments
  45. if nargin==1,
  46. DIM=[];
  47. fun=[];
  48. elseif nargin==2,
  49. if ~isnumeric(DIM),
  50. fun=DIM;
  51. DIM=[];
  52. else
  53. fun=[];
  54. end
  55. end
  56. % obtain which DIMENSION should be used
  57. if isempty(DIM),
  58. DIM=min(find(size(i)>1));
  59. if isempty(DIM), DIM=1; end;
  60. end;
  61. [S,N] = sumskipnan(i,DIM); % sum
  62. i = i - repmat(S./N,size(i)./size(S)); % remove mean
  63. [S,N] = sumskipnan(abs(i),DIM); %
  64. if flag_implicit_unbiased_estim;
  65. n1 = max(N-1,0); % in case of n=0 and n=1, the (biased) variance, STD and STE are INF
  66. else
  67. n1 = N;
  68. end;
  69. R = S./n1;