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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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. |
|
41 ## @end deftypefn |
5053
|
42 ## |
3407
|
43 ## @seealso{bar} |
724
|
44 |
2314
|
45 ## Author: jwe |
|
46 |
3597
|
47 function [nn, xx] = hist (y, x, norm) |
724
|
48 |
3690
|
49 if (nargin < 1 || nargin > 3) |
3597
|
50 usage ("[nn, xx] = hist (y, x, norm)"); |
724
|
51 endif |
2325
|
52 |
5065
|
53 arg_is_vector = is_vector (y); |
|
54 |
|
55 if (rows (y) == 1) |
4880
|
56 y = y(:); |
|
57 endif |
|
58 |
|
59 if (isreal (y)) |
724
|
60 max_val = max (y); |
|
61 min_val = min (y); |
|
62 else |
|
63 error ("hist: first argument must be a vector"); |
|
64 endif |
|
65 |
|
66 if (nargin == 1) |
|
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 |
4030
|
72 if (isscalar (x)) |
724
|
73 n = x; |
|
74 if (n <= 0) |
|
75 error ("hist: number of bins must be positive"); |
|
76 endif |
4880
|
77 x = [0.5:n]'/n; |
|
78 x = x * (max_val - min_val) + ones(size(x)) * min_val; |
|
79 elseif (isreal (x)) |
|
80 if (isvector (x)) |
|
81 x = x(:); |
|
82 endif |
724
|
83 tmp = sort (x); |
|
84 if (any (tmp != x)) |
904
|
85 warning ("hist: bin values not sorted on input"); |
724
|
86 x = tmp; |
|
87 endif |
|
88 else |
|
89 error ("hist: second argument must be a scalar or a vector"); |
|
90 endif |
|
91 endif |
|
92 |
4880
|
93 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2; |
|
94 n = rows (x); |
|
95 if (n < 30 && columns (x) == 1) |
4407
|
96 ## The following algorithm works fastest for n less than about 30. |
4880
|
97 chist = zeros (n+1, columns (y)); |
4407
|
98 for i = 1:n-1 |
4880
|
99 chist(i+1,:) = sum (y <= cutoff(i)); |
4407
|
100 endfor |
4880
|
101 chist(n+1,:) = rows (y); |
4407
|
102 else |
|
103 ## The following algorithm works fastest for n greater than about 30. |
|
104 ## Put cutoff elements between boundaries, integrate over all |
|
105 ## elements, keep totals at boundaries. |
4880
|
106 [s, idx] = sort ([y; cutoff]); |
|
107 len = rows (y); |
|
108 chist = cumsum (idx <= len); |
|
109 t1 = zeros (1, columns (y)); |
|
110 t2 = reshape (chist(idx > len), size (cutoff)); |
|
111 t3 = chist(end,:); |
|
112 chist = [t1; t2; t3]; |
4407
|
113 endif |
|
114 |
4880
|
115 freq = diff (chist); |
724
|
116 |
3597
|
117 if (nargin == 3) |
|
118 ## Normalise the histogram. |
4880
|
119 freq = freq / rows (y) * norm; |
3597
|
120 endif |
|
121 |
3175
|
122 if (nargout > 0) |
5065
|
123 if (arg_is_vector) |
4880
|
124 nn = freq'; |
|
125 xx = x'; |
|
126 else |
|
127 nn = freq; |
|
128 xx = x; |
|
129 endif |
736
|
130 else |
|
131 bar (x, freq); |
724
|
132 endif |
|
133 |
|
134 endfunction |
4811
|
135 |
|
136 %!test |
4880
|
137 %! [nn,xx]=hist([1:4],3); |
|
138 %! assert(xx, [1.5,2.5,3.5]); |
|
139 %! assert(nn, [2,1,1]); |
|
140 %!test |
|
141 %! [nn,xx]=hist([1:4]',3); |
|
142 %! assert(xx, [1.5,2.5,3.5]'); |
|
143 %! assert(nn, [2,1,1]'); |
|
144 %!test |
|
145 %! [nn,xx]=hist([[1:4]',[1:4]'],3); |
|
146 %! assert(xx, [[1.5,2.5,3.5]',[1.5,2.5,3.5]']); |
|
147 %! assert(nn, [[2,1,1]',[2,1,1]']); |
|
148 %!assert(hist(1,1),1); |
|
149 %!test |
|
150 %! for n = [10, 30, 100, 1000] |
|
151 %! assert( sum(hist([1:n], n)), n ); |
|
152 %! assert( sum(hist([1:n], [2:n-1])), n); |
|
153 %! assert( sum(hist([1:n], [1:n])), n ); |
|
154 %! assert( sum(hist([1:n], 29)), n); |
|
155 %! assert( sum(hist([1:n], 30)), n); |
4811
|
156 %! endfor |