Mercurial > hg > octave-lyh
annotate scripts/plot/hist.m @ 14872:c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
* gradient.m, interp1q.m, rat.m, tsearchn.m, image.m, imwrite.m, area.m,
contourc.m, hist.m, isocolors.m, isonormals.m, meshz.m, print.m, __bar__.m,
__go_draw_axes__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m,
__print_parse_opts__.m, __quiver__.m, rose.m, shrinkfaces.m, stairs.m,
surfnorm.m, tetramesh.m, text.m, deconv.m, spline.m, intersect.m, setdiff.m,
setxor.m, union.m, periodogram.m, pcg.m, perms.m: Replace size (x,1) with
rows (x) and size(x,2) with columns(x).
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 17 Jul 2012 13:34:19 -0700 |
parents | 5d3a684236b0 |
children | eaab03308c0b |
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 |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
54 ## hist (randn (1, 100), 25, "facecolor", "r", "edgecolor", "b"); |
11351
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 |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 ## hist (rand (10, 3)); |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
64 ## colormap (summer ()); |
11351
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; |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14613
diff
changeset
|
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++}; |
14613
e7c8e31f8e5d
hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14612
diff
changeset
|
152 freq = freq / sum(! isnan (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 | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
163 elseif (columns (freq) != 1) |
7112 | 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 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
171 |
4811 | 172 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
173 %! [nn,xx] = hist ([1:4], 3); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
174 %! assert (xx, [1.5,2.5,3.5]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
175 %! assert (nn, [2,1,1]); |
4880 | 176 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
177 %! [nn,xx] = hist ([1:4]', 3); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
178 %! assert (xx, [1.5,2.5,3.5]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
179 %! assert (nn, [2,1,1]); |
4880 | 180 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
181 %! [nn,xx] = hist ([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
182 %! assert (xx, [1,2,3]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
183 %! assert (nn, [3,2,1]); |
5746 | 184 %!test |
14613
e7c8e31f8e5d
hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14612
diff
changeset
|
185 %! [nn,xx] = hist ([1 1 1 NaN NaN NaN 2 2 3],[1 2 3], 6); |
e7c8e31f8e5d
hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14612
diff
changeset
|
186 %! assert (xx, [1,2,3]); |
e7c8e31f8e5d
hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14612
diff
changeset
|
187 %! assert (nn, [3,2,1]); |
e7c8e31f8e5d
hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14612
diff
changeset
|
188 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
189 %! [nn,xx] = hist ([[1:4]', [1:4]'], 3); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
190 %! assert (xx, [1.5;2.5;3.5]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
191 %! assert (nn, [[2,1,1]',[2,1,1]']); |
4880 | 192 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
193 %! for n = [10, 30, 100, 1000] |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
194 %! assert (sum (hist ([1:n], n)), n); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
195 %! assert (sum (hist ([1:n], [2:n-1])), n); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
196 %! assert (sum (hist ([1:n], [1:n])), n); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
197 %! assert (sum (hist ([1:n], 29)), n); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
198 %! assert (sum (hist ([1:n], 30)), n); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
199 %! endfor |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
200 %!assert (hist (1,1), 1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
201 %!assert (size (hist (randn (750,240), 200)), [200,240]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
202 |