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 -*- |
|
21 ## @deftypefn {Function File} {} hist (@var{y}, @var{x}) |
2311
|
22 ## Produce histogram counts or plots. |
3368
|
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. |
3368
|
27 ## |
2311
|
28 ## Given a second scalar argument, use that as the number of bins. |
3368
|
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. |
3368
|
33 ## |
2311
|
34 ## Extreme values are lumped in the first and last bins. |
3368
|
35 ## |
|
36 ## With two output arguments, produce the values @var{nn} and @var{xx} such |
|
37 ## that @code{bar (@var{xx}, @var{nn})} will plot the histogram. |
|
38 ## @end deftypefn |
3407
|
39 ## @seealso{bar} |
724
|
40 |
2314
|
41 ## Author: jwe |
|
42 |
2311
|
43 function [nn, xx] = hist (y, x) |
724
|
44 |
|
45 if (nargin < 1 || nargin > 2) |
904
|
46 usage ("[nn, xx] = hist (y, x)"); |
724
|
47 endif |
2325
|
48 |
724
|
49 if (is_vector (y)) |
|
50 max_val = max (y); |
|
51 min_val = min (y); |
|
52 else |
|
53 error ("hist: first argument must be a vector"); |
|
54 endif |
|
55 |
|
56 if (nargin == 1) |
|
57 n = 10; |
|
58 delta = (max_val - min_val) / n / 2; |
|
59 x = linspace (min_val+delta, max_val-delta, n); |
|
60 cutoff = x + delta; |
|
61 elseif (nargin == 2) |
|
62 if (is_scalar (x)) |
|
63 n = x; |
|
64 if (n <= 0) |
|
65 error ("hist: number of bins must be positive"); |
|
66 endif |
|
67 delta = (max_val - min_val) / n / 2; |
|
68 x = linspace (min_val+delta, max_val-delta, n); |
|
69 cutoff = x + delta; |
|
70 elseif (is_vector (x)) |
|
71 tmp = sort (x); |
|
72 if (any (tmp != x)) |
904
|
73 warning ("hist: bin values not sorted on input"); |
724
|
74 x = tmp; |
|
75 endif |
|
76 n = length (x); |
|
77 cutoff = zeros (1, n-1); |
|
78 for i = 1:n-1 |
|
79 cutoff (i) = (x (i) + x (i+1)) / 2; |
|
80 endfor |
|
81 else |
|
82 error ("hist: second argument must be a scalar or a vector"); |
|
83 endif |
|
84 endif |
|
85 |
|
86 freq = zeros (1, n); |
|
87 freq (1) = sum (y < cutoff (1)); |
|
88 for i = 2:n-1 |
|
89 freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i)); |
|
90 endfor |
|
91 freq (n) = sum (y >= cutoff (n-1)); |
|
92 |
3175
|
93 if (nargout > 0) |
736
|
94 nn = freq; |
|
95 xx = x; |
|
96 else |
|
97 bar (x, freq); |
724
|
98 endif |
|
99 |
|
100 endfunction |