2847
|
1 ## Copyright (C) 1996, 1997 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 |
3597
|
46 function [nn, xx] = hist (y, x, norm) |
724
|
47 |
3690
|
48 if (nargin < 1 || nargin > 3) |
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)) |
724
|
59 max_val = max (y); |
|
60 min_val = min (y); |
|
61 else |
|
62 error ("hist: first argument must be a vector"); |
|
63 endif |
|
64 |
|
65 if (nargin == 1) |
|
66 n = 10; |
4880
|
67 x = [0.5:n]'/n; |
|
68 x = x * (max_val - min_val) + ones(size(x)) * min_val; |
3597
|
69 else |
|
70 ## nargin is either 2 or 3 |
4030
|
71 if (isscalar (x)) |
724
|
72 n = x; |
|
73 if (n <= 0) |
|
74 error ("hist: number of bins must be positive"); |
|
75 endif |
4880
|
76 x = [0.5:n]'/n; |
|
77 x = x * (max_val - min_val) + ones(size(x)) * min_val; |
|
78 elseif (isreal (x)) |
|
79 if (isvector (x)) |
|
80 x = x(:); |
|
81 endif |
724
|
82 tmp = sort (x); |
|
83 if (any (tmp != x)) |
904
|
84 warning ("hist: bin values not sorted on input"); |
724
|
85 x = tmp; |
|
86 endif |
|
87 else |
|
88 error ("hist: second argument must be a scalar or a vector"); |
|
89 endif |
|
90 endif |
|
91 |
4880
|
92 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
|
93 n = rows (x); |
|
94 if (n < 30 && columns (x) == 1) |
4407
|
95 ## The following algorithm works fastest for n less than about 30. |
4880
|
96 chist = zeros (n+1, columns (y)); |
4407
|
97 for i = 1:n-1 |
4880
|
98 chist(i+1,:) = sum (y <= cutoff(i)); |
4407
|
99 endfor |
5746
|
100 chist(n+1,:) = sum (! isnan (y)); |
4407
|
101 else |
|
102 ## The following algorithm works fastest for n greater than about 30. |
|
103 ## Put cutoff elements between boundaries, integrate over all |
|
104 ## elements, keep totals at boundaries. |
4880
|
105 [s, idx] = sort ([y; cutoff]); |
|
106 len = rows (y); |
|
107 chist = cumsum (idx <= len); |
|
108 t1 = zeros (1, columns (y)); |
|
109 t2 = reshape (chist(idx > len), size (cutoff)); |
5746
|
110 t3 = chist(end,:) - sum (isnan (y)); |
4880
|
111 chist = [t1; t2; t3]; |
4407
|
112 endif |
|
113 |
4880
|
114 freq = diff (chist); |
724
|
115 |
3597
|
116 if (nargin == 3) |
|
117 ## Normalise the histogram. |
4880
|
118 freq = freq / rows (y) * norm; |
3597
|
119 endif |
|
120 |
6582
|
121 if (nargout > 1) |
5065
|
122 if (arg_is_vector) |
4880
|
123 nn = freq'; |
|
124 xx = x'; |
|
125 else |
|
126 nn = freq; |
|
127 xx = x; |
|
128 endif |
6582
|
129 elseif (nargout == 1) |
|
130 nn = bar (x, freq, 1.0); |
736
|
131 else |
6582
|
132 bar (x, freq, 1.0); |
724
|
133 endif |
|
134 |
|
135 endfunction |
4811
|
136 |
|
137 %!test |
4880
|
138 %! [nn,xx]=hist([1:4],3); |
|
139 %! assert(xx, [1.5,2.5,3.5]); |
|
140 %! assert(nn, [2,1,1]); |
|
141 %!test |
|
142 %! [nn,xx]=hist([1:4]',3); |
5584
|
143 %! assert(xx, [1.5,2.5,3.5]); |
|
144 %! assert(nn, [2,1,1]); |
4880
|
145 %!test |
5746
|
146 %! [nn,xx]=hist([1 1 1 NaN NaN NaN 2 2 3],[1 2 3]); |
|
147 %! assert(xx, [1,2,3]); |
|
148 %! assert(nn, [3,2,1]); |
|
149 %!test |
4880
|
150 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
|
151 %! assert(xx, [[1.5,2.5,3.5]',[1.5,2.5,3.5]']); |
|
152 %! assert(nn, [[2,1,1]',[2,1,1]']); |
|
153 %!assert(hist(1,1),1); |
|
154 %!test |
|
155 %! for n = [10, 30, 100, 1000] |
|
156 %! assert( sum(hist([1:n], n)), n ); |
|
157 %! assert( sum(hist([1:n], [2:n-1])), n); |
|
158 %! assert( sum(hist([1:n], [1:n])), n ); |
|
159 %! assert( sum(hist([1:n], 29)), n); |
|
160 %! assert( sum(hist([1:n], 30)), n); |
4811
|
161 %! endfor |