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