home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / base / mean.m < prev    next >
Text File  |  1997-02-19  |  2KB  |  72 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  mean (x [, opt])
  18. ##
  19. ## For vector arguments, return the mean the values.
  20. ## For matrix arguments, return a row vector containing the mean for
  21. ## each column.
  22. ##
  23. ## With the optional argument opt, the kind of mean computed can be
  24. ## selected.
  25. ## If opt is "a", the (ordinary) arithmetic mean is computed.  This
  26. ## is the default.
  27. ## If opt is "g", the geometric mean is computed.
  28. ## If opt is "h", the harmonic mean is computed.
  29.   
  30. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  31. ## Description:  Compute arithmetic, geometric, and harmonic mean
  32.  
  33. function y = mean (x, opt)
  34.  
  35.   if ((nargin < 1) || (nargin > 2))
  36.     usage ("mean (x [, opt])");
  37.   endif
  38.  
  39.   if isempty (x)
  40.     error ("mean:  x must not be empty");
  41.   endif
  42.   
  43.   if (rows (x) == 1)
  44.     x = x';
  45.   endif
  46.   
  47.   if (nargin == 1)
  48.     opt = "a";
  49.   endif
  50.  
  51.   [r, c] = size (x);
  52.   
  53.   if (strcmp (opt, "a"))
  54.     y = sum (x) / r;
  55.   elseif (strcmp (opt, "g"))
  56.     y = NaN * ones (1, c);
  57.     i = find (all (x > 0));
  58.     if any (i)
  59.       y(i) = exp (sum (log (x(:, i))) / r);
  60.     endif
  61.   elseif (strcmp (opt, "h"))
  62.     y = NaN * ones (1, c);
  63.     i = find (all (x != 0));
  64.     if any (i)
  65.       y(i) = r ./ sum (1 ./ x(:, i));
  66.     endif
  67.   else
  68.     error (sprintf ("mean:  option `%s' not recognized", opt));
  69.   endif
  70.     
  71. endfunction
  72.