Mercurial > hg > octave-nkf
annotate scripts/plot/hist.m @ 8710:739141cde75a ss-3-1-52
fix typo in Array-f.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 09 Feb 2009 21:51:31 +0100 |
parents | cadc73247d65 |
children | eb63fbe60fab |
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 | |
8427
26b899d309f6
help and error string corrected in hist.m.
Francesco Potortì <pot@gnu.org>
parents:
7566
diff
changeset
|
26 ## of the data. With one matrix input argument, plot a hystogram where |
26b899d309f6
help and error string corrected in hist.m.
Francesco Potortì <pot@gnu.org>
parents:
7566
diff
changeset
|
27 ## each bin contains a bar per input column. |
3426 | 28 ## |
2311 | 29 ## Given a second scalar argument, use that as the number of bins. |
3426 | 30 ## |
2311 | 31 ## Given a second vector argument, use that as the centers of the bins, |
3368 | 32 ## with the width of the bins determined from the adjacent values in |
2311 | 33 ## the vector. |
3426 | 34 ## |
3597 | 35 ## If third argument is provided, the histogram is normalised such that |
36 ## the sum of the bars is equal to @var{norm}. | |
37 ## | |
2311 | 38 ## Extreme values are lumped in the first and last bins. |
3426 | 39 ## |
3368 | 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. | |
5642 | 42 ## @seealso{bar} |
3368 | 43 ## @end deftypefn |
724 | 44 |
2314 | 45 ## Author: jwe |
46 | |
7112 | 47 function [nn, xx] = hist (y, varargin) |
724 | 48 |
7112 | 49 if (nargin < 1) |
6046 | 50 print_usage (); |
724 | 51 endif |
2325 | 52 |
5443 | 53 arg_is_vector = isvector (y); |
5065 | 54 |
55 if (rows (y) == 1) | |
4880 | 56 y = y(:); |
57 endif | |
58 | |
59 if (isreal (y)) | |
7112 | 60 max_val = max (y(:)); |
61 min_val = min (y(:)); | |
724 | 62 else |
8427
26b899d309f6
help and error string corrected in hist.m.
Francesco Potortì <pot@gnu.org>
parents:
7566
diff
changeset
|
63 error ("hist: first argument must be real valued"); |
724 | 64 endif |
65 | |
7112 | 66 iarg = 1; |
67 if (nargin == 1 || ischar (varargin{iarg})) | |
724 | 68 n = 10; |
4880 | 69 x = [0.5:n]'/n; |
70 x = x * (max_val - min_val) + ones(size(x)) * min_val; | |
3597 | 71 else |
72 ## nargin is either 2 or 3 | |
7208 | 73 x = varargin{iarg++}; |
4030 | 74 if (isscalar (x)) |
724 | 75 n = x; |
76 if (n <= 0) | |
77 error ("hist: number of bins must be positive"); | |
78 endif | |
4880 | 79 x = [0.5:n]'/n; |
7191 | 80 x = x * (max_val - min_val) + ones (size (x)) * min_val; |
4880 | 81 elseif (isreal (x)) |
82 if (isvector (x)) | |
83 x = x(:); | |
84 endif | |
724 | 85 tmp = sort (x); |
86 if (any (tmp != x)) | |
904 | 87 warning ("hist: bin values not sorted on input"); |
724 | 88 x = tmp; |
89 endif | |
90 else | |
91 error ("hist: second argument must be a scalar or a vector"); | |
92 endif | |
93 endif | |
94 | |
7189 | 95 ## Avoid issues with integer types for x and y |
96 x = double (x); | |
97 y = double (y); | |
98 | |
4880 | 99 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
100 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
|
101 y_nc = columns (y); |
4880 | 102 if (n < 30 && columns (x) == 1) |
4407 | 103 ## 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
|
104 chist = zeros (n+1, y_nc); |
4407 | 105 for i = 1:n-1 |
4880 | 106 chist(i+1,:) = sum (y <= cutoff(i)); |
4407 | 107 endfor |
5746 | 108 chist(n+1,:) = sum (! isnan (y)); |
4407 | 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. | |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
113 [s, idx] = sort ([y; repmat(cutoff, 1, y_nc)]); |
4880 | 114 len = rows (y); |
115 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
|
116 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
|
117 (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
|
118 (chist(end,:) - sum (isnan (y)))]; |
4407 | 119 endif |
120 | |
4880 | 121 freq = diff (chist); |
724 | 122 |
7191 | 123 if (nargin > 2 && ! ischar (varargin{iarg})) |
3597 | 124 ## Normalise the histogram. |
7112 | 125 norm = varargin{iarg++}; |
4880 | 126 freq = freq / rows (y) * norm; |
3597 | 127 endif |
128 | |
6586 | 129 if (nargout > 0) |
5065 | 130 if (arg_is_vector) |
4880 | 131 nn = freq'; |
132 xx = x'; | |
133 else | |
134 nn = freq; | |
135 xx = x; | |
136 endif | |
7112 | 137 elseif (size (freq, 2) != 1) |
138 bar (x, freq, 0.8, varargin{iarg:end}); | |
736 | 139 else |
7112 | 140 bar (x, freq, 1.0, varargin{iarg:end}); |
724 | 141 endif |
142 | |
143 endfunction | |
4811 | 144 |
145 %!test | |
4880 | 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); | |
5584 | 151 %! assert(xx, [1.5,2.5,3.5]); |
152 %! assert(nn, [2,1,1]); | |
4880 | 153 %!test |
5746 | 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 | |
4880 | 158 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
7112 | 159 %! assert(xx, [1.5;2.5;3.5]); |
4880 | 160 %! assert(nn, [[2,1,1]',[2,1,1]']); |
161 %!assert(hist(1,1),1); | |
162 %!test | |
163 %! for n = [10, 30, 100, 1000] | |
8507 | 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); | |
4811 | 169 %! endfor |
7566
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
170 %!test |
b3acdf1c41a5
hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents:
7208
diff
changeset
|
171 %! assert (size (hist(randn(750,240), 200)), [200,240]); |