home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXY.DI$ / HIST.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.5 KB  |  63 lines

  1. function [no,xo] = hist(y,x)
  2. %HIST    Plot histograms.
  3. %    HIST(Y) plots a histogram with 10 equally spaced bins between
  4. %    the minimum and maximum values in Y, showing the distribution
  5. %    of the elements in vector Y.
  6. %    HIST(Y,N), where N is a scalar, uses N bins.
  7. %    HIST(Y,X), where X is a vector, draws a histogram using the
  8. %    bins specified in X.
  9. %    [N,X] = HIST(...) does not draw a graph, but returns vectors
  10. %    X and N such that BAR(X,N) is the histogram.
  11. %
  12. %    See also BAR.
  13.  
  14. %    J.N. Little 2-06-86
  15. %    Revised 10-29-87, 12-29-88 LS
  16. %    Revised 8-13-91 by cmt, 2-3-92 by ls.
  17. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  18.  
  19. if nargin == 0
  20.     error('Requires one or two input arguments.')
  21. end
  22. if nargin == 1
  23.     x = 10;
  24. end
  25. if min(size(y))==1, y = y(:); end
  26. if isstr(x) | isstr(y)
  27.     error('Input arguments must be numeric.')
  28. end
  29. [m,n] = size(y);
  30. if max(size(x)) == 1
  31.     miny = min(min(y));
  32.     maxy = max(max(y));
  33.     binwidth = (maxy - miny) ./ x;
  34.     xx = miny + binwidth*[0:x];
  35.     xx(length(xx)) = maxy;
  36.     x = xx(1:length(xx)-1) + binwidth/2;
  37. else
  38.     xx = x(:)';
  39.     miny = min(min(y));
  40.     maxy = max(max(y));
  41.     binwidth = [diff(xx) 0];
  42.     xx = [xx(1)-binwidth(1)/2 xx+binwidth/2];
  43.     xx(1) = miny;
  44.     xx(length(xx)) = maxy;
  45. end
  46. nbin = max(size(xx));
  47. nn = zeros(nbin,n);
  48. for i=2:nbin
  49.     nn(i,:) = sum(y <= xx(i));
  50. end
  51. nn = nn(2:nbin,:) - nn(1:nbin-1,:);
  52. if nargout == 0
  53.     bar(x,nn);
  54. else
  55.   if min(size(y))==1, % Return row vectors if possible.
  56.     no = nn';
  57.     xo = x;
  58.   else
  59.     no = nn;
  60.     xo = x';
  61.   end
  62. end
  63.