1## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
2## 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {} hist (@var{y}, @var{x}, @var{norm})
22## Produce histogram counts or plots.
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. With one matrix input argument, plot a histogram where
27## each bin contains a bar per input column.
29## Given a second scalar argument, use that as the number of bins.
31## Given a second vector argument, use that as the centers of the bins,
32## with the width of the bins determined from the adjacent values in
35## If third argument is provided, the histogram is normalized such that
36## the sum of the bars is equal to @var{norm}.
38## Extreme values are lumped in the first and last bins.
40## With two output arguments, produce the values @var{nn} and @var{xx} such
41## that @code{bar (@var{xx}, @var{nn})} will plot the histogram.
47function [nn, xx] = hist (y, varargin)
53 arg_is_vector = isvector (y);
63 error ("hist: first argument must be real valued");
67 if (nargin == 1 || ischar (varargin{iarg}))
70 x = x * (max_val - min_val) + ones(size(x)) * min_val;
72 ## nargin is either 2 or 3
77 error ("hist: number of bins must be positive");
80 x = x * (max_val - min_val) + ones (size (x)) * min_val;
87 warning ("hist: bin values not sorted on input");
91 error ("hist: second argument must be a scalar or a vector");
95 ## Avoid issues with integer types for x and y
99 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2;
102 if (n < 30 && columns (x) == 1)
103 ## The following algorithm works fastest for n less than about 30.
104 chist = zeros (n+1, y_nc);
106 chist(i+1,:) = sum (y <= cutoff(i));
108 chist(n+1,:) = sum (! isnan (y));
110 ## The following algorithm works fastest for n greater than about 30.
111 ## Put cutoff elements between boundaries, integrate over all
112 ## elements, keep totals at boundaries.
113 [s, idx] = sort ([y; repmat(cutoff, 1, y_nc)]);
115 chist = cumsum (idx <= len);
116 chist = [(zeros (1, y_nc));
117 (reshape (chist(idx > len), rows (cutoff), y_nc));
118 (chist(end,:) - sum (isnan (y)))];
123 if (nargin > 2 && ! ischar (varargin{iarg}))
124 ## Normalise the histogram.
125 norm = varargin{iarg++};
126 freq = freq / rows (y) * norm;
137 elseif (size (freq, 2) != 1)
138 bar (x, freq, 0.8, varargin{iarg:end});
140 bar (x, freq, 1.0, varargin{iarg:end});
146%! [nn,xx]=hist([1:4],3);
147%! assert(xx, [1.5,2.5,3.5]);
148%! assert(nn, [2,1,1]);
150%! [nn,xx]=hist([1:4]',3);
151%! assert(xx, [1.5,2.5,3.5]);
152%! assert(nn, [2,1,1]);
154%! [nn,xx]=hist([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]);
155%! assert(xx, [1,2,3]);
156%! assert(nn, [3,2,1]);
158%! [nn,xx]=hist([[1:4]',[1:4]'],3);
159%! assert(xx, [1.5;2.5;3.5]);
160%! assert(nn, [[2,1,1]',[2,1,1]']);
161%!assert(hist(1,1),1);
163%! for n = [10, 30, 100, 1000]
164%! assert(sum(hist([1:n], n)), n);
165%! assert(sum(hist([1:n], [2:n-1])), n);
166%! assert(sum(hist([1:n], [1:n])), n);
167%! assert(sum(hist([1:n], 29)), n);
168%! assert(sum(hist([1:n], 30)), n);
171%! assert (size (hist(randn(750,240), 200)), [200,240]);