Mercurial > hg > octave-lyh
annotate scripts/plot/hist.m @ 7566:b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 06 Mar 2008 13:42:08 -0500 |
parents | a730e47fda4d |
children | 26b899d309f6 72830070a17b |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
2 ## 2004, 2005, 2006, 2007 John W. Eaton | |
2313 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
724 | 19 |
3368 | 20 ## -*- texinfo -*- |
3597 | 21 ## @deftypefn {Function File} {} hist (@var{y}, @var{x}, @var{norm}) |
2311 | 22 ## Produce histogram counts or plots. |
3426 | 23 ## |
2311 | 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. | |
3426 | 27 ## |
2311 | 28 ## Given a second scalar argument, use that as the number of bins. |
3426 | 29 ## |
2311 | 30 ## Given a second vector argument, use that as the centers of the bins, |
3368 | 31 ## with the width of the bins determined from the adjacent values in |
2311 | 32 ## the vector. |
3426 | 33 ## |
3597 | 34 ## If third argument is provided, the histogram is normalised such that |
35 ## the sum of the bars is equal to @var{norm}. | |
36 ## | |
2311 | 37 ## Extreme values are lumped in the first and last bins. |
3426 | 38 ## |
3368 | 39 ## With two output arguments, produce the values @var{nn} and @var{xx} such |
40 ## that @code{bar (@var{xx}, @var{nn})} will plot the histogram. | |
5642 | 41 ## @seealso{bar} |
3368 | 42 ## @end deftypefn |
724 | 43 |
2314 | 44 ## Author: jwe |
45 | |
7112 | 46 function [nn, xx] = hist (y, varargin) |
724 | 47 |
7112 | 48 if (nargin < 1) |
6046 | 49 print_usage (); |
724 | 50 endif |
2325 | 51 |
5443 | 52 arg_is_vector = isvector (y); |
5065 | 53 |
54 if (rows (y) == 1) | |
4880 | 55 y = y(:); |
56 endif | |
57 | |
58 if (isreal (y)) | |
7112 | 59 max_val = max (y(:)); |
60 min_val = min (y(:)); | |
724 | 61 else |
62 error ("hist: first argument must be a vector"); | |
63 endif | |
64 | |
7112 | 65 iarg = 1; |
66 if (nargin == 1 || ischar (varargin{iarg})) | |
724 | 67 n = 10; |
4880 | 68 x = [0.5:n]'/n; |
69 x = x * (max_val - min_val) + ones(size(x)) * min_val; | |
3597 | 70 else |
71 ## nargin is either 2 or 3 | |
7208 | 72 x = varargin{iarg++}; |
4030 | 73 if (isscalar (x)) |
724 | 74 n = x; |
75 if (n <= 0) | |
76 error ("hist: number of bins must be positive"); | |
77 endif | |
4880 | 78 x = [0.5:n]'/n; |
7191 | 79 x = x * (max_val - min_val) + ones (size (x)) * min_val; |
4880 | 80 elseif (isreal (x)) |
81 if (isvector (x)) | |
82 x = x(:); | |
83 endif | |
724 | 84 tmp = sort (x); |
85 if (any (tmp != x)) | |
904 | 86 warning ("hist: bin values not sorted on input"); |
724 | 87 x = tmp; |
88 endif | |
89 else | |
90 error ("hist: second argument must be a scalar or a vector"); | |
91 endif | |
92 endif | |
93 | |
7189 | 94 ## Avoid issues with integer types for x and y |
95 x = double (x); | |
96 y = double (y); | |
97 | |
4880 | 98 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
99 n = rows (x); | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
100 y_nc = columns (y); |
4880 | 101 if (n < 30 && columns (x) == 1) |
4407 | 102 ## The following algorithm works fastest for n less than about 30. |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
103 chist = zeros (n+1, y_nc); |
4407 | 104 for i = 1:n-1 |
4880 | 105 chist(i+1,:) = sum (y <= cutoff(i)); |
4407 | 106 endfor |
5746 | 107 chist(n+1,:) = sum (! isnan (y)); |
4407 | 108 else |
109 ## The following algorithm works fastest for n greater than about 30. | |
110 ## Put cutoff elements between boundaries, integrate over all | |
111 ## elements, keep totals at boundaries. | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
112 [s, idx] = sort ([y; repmat(cutoff, 1, y_nc)]); |
4880 | 113 len = rows (y); |
114 chist = cumsum (idx <= len); | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
115 chist = [(zeros (1, y_nc)); |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
116 (reshape (chist(idx > len), rows (cutoff), y_nc)); |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
117 (chist(end,:) - sum (isnan (y)))]; |
4407 | 118 endif |
119 | |
4880 | 120 freq = diff (chist); |
724 | 121 |
7191 | 122 if (nargin > 2 && ! ischar (varargin{iarg})) |
3597 | 123 ## Normalise the histogram. |
7112 | 124 norm = varargin{iarg++}; |
4880 | 125 freq = freq / rows (y) * norm; |
3597 | 126 endif |
127 | |
6586 | 128 if (nargout > 0) |
5065 | 129 if (arg_is_vector) |
4880 | 130 nn = freq'; |
131 xx = x'; | |
132 else | |
133 nn = freq; | |
134 xx = x; | |
135 endif | |
7112 | 136 elseif (size (freq, 2) != 1) |
137 bar (x, freq, 0.8, varargin{iarg:end}); | |
736 | 138 else |
7112 | 139 bar (x, freq, 1.0, varargin{iarg:end}); |
724 | 140 endif |
141 | |
142 endfunction | |
4811 | 143 |
144 %!test | |
4880 | 145 %! [nn,xx]=hist([1:4],3); |
146 %! assert(xx, [1.5,2.5,3.5]); | |
147 %! assert(nn, [2,1,1]); | |
148 %!test | |
149 %! [nn,xx]=hist([1:4]',3); | |
5584 | 150 %! assert(xx, [1.5,2.5,3.5]); |
151 %! assert(nn, [2,1,1]); | |
4880 | 152 %!test |
5746 | 153 %! [nn,xx]=hist([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]); |
154 %! assert(xx, [1,2,3]); | |
155 %! assert(nn, [3,2,1]); | |
156 %!test | |
4880 | 157 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
7112 | 158 %! assert(xx, [1.5;2.5;3.5]); |
4880 | 159 %! assert(nn, [[2,1,1]',[2,1,1]']); |
160 %!assert(hist(1,1),1); | |
161 %!test | |
162 %! for n = [10, 30, 100, 1000] | |
163 %! assert( sum(hist([1:n], n)), n ); | |
164 %! assert( sum(hist([1:n], [2:n-1])), n); | |
165 %! assert( sum(hist([1:n], [1:n])), n ); | |
166 %! assert( sum(hist([1:n], 29)), n); | |
167 %! assert( sum(hist([1:n], 30)), n); | |
4811 | 168 %! endfor |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
169 %!test |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
170 %! assert (size (hist(randn(750,240), 200)), [200,240]); |