Mercurial > hg > octave-lyh
annotate scripts/plot/hist.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | c792872f8942 |
children | 4d917a6a858b |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
724 | 18 |
3368 | 19 ## -*- texinfo -*- |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} hist (@var{y}) |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {} hist (@var{y}, @var{x}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {} hist (@var{y}, @var{nbins}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
23 ## @deftypefnx {Function File} {} hist (@var{y}, @var{x}, @var{norm}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{nn}, @var{xx}] =} hist (@dots{}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
25 ## @deftypefnx {Function File} {[@dots{}] =} hist (@dots{}, @var{prop}, @var{val}) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
26 ## |
2311 | 27 ## Produce histogram counts or plots. |
3426 | 28 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
29 ## With one vector input argument, @var{y}, plot a histogram of the values |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
30 ## with 10 bins. The range of the histogram bins is determined by the |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
31 ## range of the data. With one matrix input argument, @var{y}, plot a |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
32 ## histogram where each bin contains a bar per input column. |
3426 | 33 ## |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
34 ## Given a second vector argument, @var{x}, use that as the centers of |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
35 ## the bins, with the width of the bins determined from the adjacent |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
36 ## values in the vector. |
3426 | 37 ## |
11575
d6619410e79c
Spellcheck documentation before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11563
diff
changeset
|
38 ## If scalar, the second argument, @var{nbins}, defines the number of bins. |
3426 | 39 ## |
11575
d6619410e79c
Spellcheck documentation before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11563
diff
changeset
|
40 ## If a third argument is provided, the histogram is normalized such that |
3597 | 41 ## the sum of the bars is equal to @var{norm}. |
42 ## | |
2311 | 43 ## Extreme values are lumped in the first and last bins. |
3426 | 44 ## |
3368 | 45 ## With two output arguments, produce the values @var{nn} and @var{xx} such |
46 ## that @code{bar (@var{xx}, @var{nn})} will plot the histogram. | |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
47 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
48 ## The histogram's appearance may be modified by specifying property/value |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
49 ## pairs, @var{prop} and @var{val} pairs. For example the face and edge |
11351
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
50 ## color may be modified. |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
51 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
52 ## @example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
53 ## @group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
54 ## hist (randn (1, 100), 25, "facecolor", "r", "edgecolor", "b") |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
55 ## @end group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
56 ## @end example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
57 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
58 ## @noindent |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
59 ## The histograms colors also depend upon the colormap. |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
60 ## |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
61 ## @example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
62 ## @group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
63 ## hist (rand (10, 3)) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
64 ## colormap (summer ()) |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
65 ## @end group |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
66 ## @end example |
bff585d759cf
improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents:
10549
diff
changeset
|
67 ## |
5642 | 68 ## @seealso{bar} |
3368 | 69 ## @end deftypefn |
724 | 70 |
2314 | 71 ## Author: jwe |
72 | |
7112 | 73 function [nn, xx] = hist (y, varargin) |
724 | 74 |
7112 | 75 if (nargin < 1) |
6046 | 76 print_usage (); |
724 | 77 endif |
2325 | 78 |
5443 | 79 arg_is_vector = isvector (y); |
5065 | 80 |
81 if (rows (y) == 1) | |
4880 | 82 y = y(:); |
83 endif | |
84 | |
85 if (isreal (y)) | |
7112 | 86 max_val = max (y(:)); |
87 min_val = min (y(:)); | |
724 | 88 else |
8427
26b899d309f6
help and error string corrected in hist.m.
Francesco Potortì <pot@gnu.org>
parents:
7566
diff
changeset
|
89 error ("hist: first argument must be real valued"); |
724 | 90 endif |
91 | |
7112 | 92 iarg = 1; |
93 if (nargin == 1 || ischar (varargin{iarg})) | |
724 | 94 n = 10; |
4880 | 95 x = [0.5:n]'/n; |
96 x = x * (max_val - min_val) + ones(size(x)) * min_val; | |
3597 | 97 else |
98 ## nargin is either 2 or 3 | |
7208 | 99 x = varargin{iarg++}; |
4030 | 100 if (isscalar (x)) |
724 | 101 n = x; |
102 if (n <= 0) | |
103 error ("hist: number of bins must be positive"); | |
104 endif | |
4880 | 105 x = [0.5:n]'/n; |
7191 | 106 x = x * (max_val - min_val) + ones (size (x)) * min_val; |
4880 | 107 elseif (isreal (x)) |
108 if (isvector (x)) | |
10549 | 109 x = x(:); |
4880 | 110 endif |
724 | 111 tmp = sort (x); |
112 if (any (tmp != x)) | |
904 | 113 warning ("hist: bin values not sorted on input"); |
724 | 114 x = tmp; |
115 endif | |
116 else | |
117 error ("hist: second argument must be a scalar or a vector"); | |
118 endif | |
119 endif | |
120 | |
7189 | 121 ## Avoid issues with integer types for x and y |
122 x = double (x); | |
123 y = double (y); | |
124 | |
4880 | 125 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
126 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
|
127 y_nc = columns (y); |
4880 | 128 if (n < 30 && columns (x) == 1) |
4407 | 129 ## 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
|
130 chist = zeros (n+1, y_nc); |
4407 | 131 for i = 1:n-1 |
4880 | 132 chist(i+1,:) = sum (y <= cutoff(i)); |
4407 | 133 endfor |
5746 | 134 chist(n+1,:) = sum (! isnan (y)); |
4407 | 135 else |
136 ## The following algorithm works fastest for n greater than about 30. | |
137 ## Put cutoff elements between boundaries, integrate over all | |
138 ## 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
|
139 [s, idx] = sort ([y; repmat(cutoff, 1, y_nc)]); |
4880 | 140 len = rows (y); |
141 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
|
142 chist = [(zeros (1, y_nc)); |
10549 | 143 (reshape (chist(idx > len), rows (cutoff), y_nc)); |
144 (chist(end,:) - sum (isnan (y)))]; | |
4407 | 145 endif |
146 | |
4880 | 147 freq = diff (chist); |
724 | 148 |
7191 | 149 if (nargin > 2 && ! ischar (varargin{iarg})) |
3597 | 150 ## Normalise the histogram. |
7112 | 151 norm = varargin{iarg++}; |
4880 | 152 freq = freq / rows (y) * norm; |
3597 | 153 endif |
154 | |
6586 | 155 if (nargout > 0) |
5065 | 156 if (arg_is_vector) |
4880 | 157 nn = freq'; |
158 xx = x'; | |
159 else | |
160 nn = freq; | |
161 xx = x; | |
162 endif | |
7112 | 163 elseif (size (freq, 2) != 1) |
164 bar (x, freq, 0.8, varargin{iarg:end}); | |
736 | 165 else |
7112 | 166 bar (x, freq, 1.0, varargin{iarg:end}); |
724 | 167 endif |
168 | |
169 endfunction | |
4811 | 170 |
171 %!test | |
4880 | 172 %! [nn,xx]=hist([1:4],3); |
173 %! assert(xx, [1.5,2.5,3.5]); | |
174 %! assert(nn, [2,1,1]); | |
175 %!test | |
176 %! [nn,xx]=hist([1:4]',3); | |
5584 | 177 %! assert(xx, [1.5,2.5,3.5]); |
178 %! assert(nn, [2,1,1]); | |
4880 | 179 %!test |
5746 | 180 %! [nn,xx]=hist([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]); |
181 %! assert(xx, [1,2,3]); | |
182 %! assert(nn, [3,2,1]); | |
183 %!test | |
4880 | 184 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
7112 | 185 %! assert(xx, [1.5;2.5;3.5]); |
4880 | 186 %! assert(nn, [[2,1,1]',[2,1,1]']); |
187 %!assert(hist(1,1),1); | |
188 %!test | |
189 %! for n = [10, 30, 100, 1000] | |
8507 | 190 %! assert(sum(hist([1:n], n)), n); |
191 %! assert(sum(hist([1:n], [2:n-1])), n); | |
192 %! assert(sum(hist([1:n], [1:n])), n); | |
193 %! assert(sum(hist([1:n], 29)), n); | |
194 %! assert(sum(hist([1:n], 30)), n); | |
4811 | 195 %! endfor |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
196 %!test |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
197 %! assert (size (hist(randn(750,240), 200)), [200,240]); |