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 |
3407
|
42 ## @seealso{bar} |
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) |
3597
|
49 usage ("[nn, xx] = hist (y, x, norm)"); |
724
|
50 endif |
2325
|
51 |
4030
|
52 if (isvector (y)) |
724
|
53 max_val = max (y); |
|
54 min_val = min (y); |
|
55 else |
|
56 error ("hist: first argument must be a vector"); |
|
57 endif |
|
58 |
|
59 if (nargin == 1) |
|
60 n = 10; |
4873
|
61 delta = (max_val - min_val) / (n-1) / 2; |
|
62 cutoff = linspace (min_val+delta, max_val-delta, n-1); |
3597
|
63 else |
|
64 ## nargin is either 2 or 3 |
4030
|
65 if (isscalar (x)) |
724
|
66 n = x; |
|
67 if (n <= 0) |
|
68 error ("hist: number of bins must be positive"); |
|
69 endif |
4873
|
70 delta = (max_val - min_val) / (n-1) / 2; |
|
71 cutoff = linspace (min_val+delta, max_val-delta, n-1); |
4030
|
72 elseif (isvector (x)) |
724
|
73 tmp = sort (x); |
|
74 if (any (tmp != x)) |
904
|
75 warning ("hist: bin values not sorted on input"); |
724
|
76 x = tmp; |
|
77 endif |
4407
|
78 cutoff = (x(1:end-1) + x(2:end)) / 2; |
4430
|
79 n = length (x); |
724
|
80 else |
|
81 error ("hist: second argument must be a scalar or a vector"); |
|
82 endif |
|
83 endif |
|
84 |
4407
|
85 if (n < 30) |
|
86 ## The following algorithm works fastest for n less than about 30. |
|
87 chist = [zeros(n,1); length(y)]; |
|
88 for i = 1:n-1 |
|
89 chist(i+1) = sum (y < cutoff(i)); |
|
90 endfor |
|
91 else |
|
92 ## The following algorithm works fastest for n greater than about 30. |
|
93 ## Put cutoff elements between boundaries, integrate over all |
|
94 ## elements, keep totals at boundaries. |
|
95 [s, idx] = sort ([cutoff(:); y(:)]); |
4811
|
96 chist = cumsum(idx>=n); |
4407
|
97 chist = [0; chist(idx<n); chist(end)]; |
|
98 endif |
|
99 |
|
100 freq= diff(chist)'; |
724
|
101 |
3597
|
102 if (nargin == 3) |
|
103 ## Normalise the histogram. |
4407
|
104 freq = freq / length (y) * norm; |
3597
|
105 endif |
|
106 |
3175
|
107 if (nargout > 0) |
736
|
108 nn = freq; |
|
109 xx = x; |
|
110 else |
|
111 bar (x, freq); |
724
|
112 endif |
|
113 |
|
114 endfunction |
4811
|
115 |
|
116 %!test |
|
117 %! for n = [1, 10, 30, 100, 1000] |
|
118 %! assert( sum(hist([1:n], [1:n])) == n ); |
|
119 %! endfor |