home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / plot / hist.m < prev    next >
Text File  |  1999-12-24  |  3KB  |  102 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} hist (@var{y}, @var{x})
  22. ## Produce histogram counts or plots.
  23. ## 
  24. ## With one vector input argument, plot a histogram of the values with
  25. ## 10 bins.  The range of the histogram bins is determined by the range
  26. ## of the data.
  27. ## 
  28. ## Given a second scalar argument, use that as the number of bins.
  29. ## 
  30. ## Given a second vector argument, use that as the centers of the bins,
  31. ## with the width of the bins determined from the adjacent values in
  32. ## the vector.
  33. ## 
  34. ## Extreme values are lumped in the first and last bins.
  35. ## 
  36. ## With two output arguments, produce the values @var{nn} and @var{xx} such
  37. ## that @code{bar (@var{xx}, @var{nn})} will plot the histogram.
  38. ## @end deftypefn
  39.  
  40. ## See also: bar
  41.  
  42. ## Author: jwe
  43.  
  44. function [nn, xx] = hist (y, x)
  45.  
  46.   if (nargin < 1 || nargin > 2)
  47.     usage ("[nn, xx] = hist (y, x)");
  48.   endif
  49.  
  50.   if (is_vec (y))
  51.     max_val = max (y);
  52.     min_val = min (y);
  53.   else
  54.     error ("hist: first argument must be a vector");
  55.   endif
  56.  
  57.   if (nargin == 1)
  58.     n = 10;
  59.     delta = (max_val - min_val) / n / 2;
  60.     x = linspace (min_val+delta, max_val-delta, n);
  61.     cutoff = x + delta;
  62.   elseif (nargin == 2)
  63.     if (is_scal (x))
  64.       n = x;
  65.       if (n <= 0)
  66.         error ("hist: number of bins must be positive");
  67.       endif
  68.       delta = (max_val - min_val) / n / 2;
  69.       x = linspace (min_val+delta, max_val-delta, n);
  70.       cutoff = x + delta;
  71.     elseif (is_vec (x))
  72.       tmp = sort (x);
  73.       if (any (tmp != x))
  74.         warning ("hist: bin values not sorted on input");
  75.         x = tmp;
  76.       endif
  77.       n = length (x);
  78.       cutoff = zeros (1, n-1);
  79.       for i = 1:n-1
  80.         cutoff (i) = (x (i) + x (i+1)) / 2;
  81.       endfor
  82.     else
  83.       error ("hist: second argument must be a scalar or a vector");
  84.     endif
  85.   endif
  86.  
  87.   freq = zeros (1, n);
  88.   freq (1) = sum (y < cutoff (1));
  89.   for i = 2:n-1
  90.     freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i));
  91.   endfor
  92.   freq (n) = sum (y >= cutoff (n-1));
  93.  
  94.   if (nargout > 0)
  95.     nn = freq;
  96.     xx = x;
  97.   else
  98.     bar (x, freq);
  99.   endif
  100.  
  101. endfunction
  102.