changelog shortlog tags changeset files revisions annotate raw

scripts/plot/hist.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (45 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
2## 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
3##
4## This file is part of Octave.
5##
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.
10##
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.
15##
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/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} hist (@var{y}, @var{x}, @var{norm})
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. With one matrix input argument, plot a histogram where
27## each bin contains a bar per input column.
28##
29## Given a second scalar argument, use that as the number of bins.
30##
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
33## the vector.
34##
35## If third argument is provided, the histogram is normalized such that
36## the sum of the bars is equal to @var{norm}.
37##
38## Extreme values are lumped in the first and last bins.
39##
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.
42## @seealso{bar}
43## @end deftypefn
44
45## Author: jwe
46
47function [nn, xx] = hist (y, varargin)
48
49 if (nargin < 1)
50 print_usage ();
51 endif
52
53 arg_is_vector = isvector (y);
54
55 if (rows (y) == 1)
56 y = y(:);
57 endif
58
59 if (isreal (y))
60 max_val = max (y(:));
61 min_val = min (y(:));
62 else
63 error ("hist: first argument must be real valued");
64 endif
65
66 iarg = 1;
67 if (nargin == 1 || ischar (varargin{iarg}))
68 n = 10;
69 x = [0.5:n]'/n;
70 x = x * (max_val - min_val) + ones(size(x)) * min_val;
71 else
72 ## nargin is either 2 or 3
73 x = varargin{iarg++};
74 if (isscalar (x))
75 n = x;
76 if (n <= 0)
77 error ("hist: number of bins must be positive");
78 endif
79 x = [0.5:n]'/n;
80 x = x * (max_val - min_val) + ones (size (x)) * min_val;
81 elseif (isreal (x))
82 if (isvector (x))
83 x = x(:);
84 endif
85 tmp = sort (x);
86 if (any (tmp != x))
87 warning ("hist: bin values not sorted on input");
88 x = tmp;
89 endif
90 else
91 error ("hist: second argument must be a scalar or a vector");
92 endif
93 endif
94
95 ## Avoid issues with integer types for x and y
96 x = double (x);
97 y = double (y);
98
99 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2;
100 n = rows (x);
101 y_nc = columns (y);
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);
105 for i = 1:n-1
106 chist(i+1,:) = sum (y <= cutoff(i));
107 endfor
108 chist(n+1,:) = sum (! isnan (y));
109 else
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)]);
114 len = rows (y);
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)))];
119 endif
120
121 freq = diff (chist);
122
123 if (nargin > 2 && ! ischar (varargin{iarg}))
124 ## Normalise the histogram.
125 norm = varargin{iarg++};
126 freq = freq / rows (y) * norm;
127 endif
128
129 if (nargout > 0)
130 if (arg_is_vector)
131 nn = freq';
132 xx = x';
133 else
134 nn = freq;
135 xx = x;
136 endif
137 elseif (size (freq, 2) != 1)
138 bar (x, freq, 0.8, varargin{iarg:end});
139 else
140 bar (x, freq, 1.0, varargin{iarg:end});
141 endif
142
143endfunction
144
145%!test
146%! [nn,xx]=hist([1:4],3);
147%! assert(xx, [1.5,2.5,3.5]);
148%! assert(nn, [2,1,1]);
149%!test
150%! [nn,xx]=hist([1:4]',3);
151%! assert(xx, [1.5,2.5,3.5]);
152%! assert(nn, [2,1,1]);
153%!test
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]);
157%!test
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);
162%!test
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);
169%! endfor
170%!test
171%! assert (size (hist(randn(750,240), 200)), [200,240]);