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 |
|
39 |
2311
|
40 ## See also: bar |
724
|
41 |
2314
|
42 ## Author: jwe |
|
43 |
2311
|
44 function [nn, xx] = hist (y, x) |
724
|
45 |
|
46 if (nargin < 1 || nargin > 2) |
904
|
47 usage ("[nn, xx] = hist (y, x)"); |
724
|
48 endif |
2325
|
49 |
724
|
50 if (is_vector (y)) |
|
51 max_val = max (y); |
|
52 min_val = min (y); |
|
53 else |
|
54 error ("hist: first argument must be a vector"); |
|
55 endif |
|
56 |
|
57 if (nargin == 1) |
|
58 n = 10; |
|
59 delta = (max_val - min_val) / n / 2; |
|
60 x = linspace (min_val+delta, max_val-delta, n); |
|
61 cutoff = x + delta; |
|
62 elseif (nargin == 2) |
|
63 if (is_scalar (x)) |
|
64 n = x; |
|
65 if (n <= 0) |
|
66 error ("hist: number of bins must be positive"); |
|
67 endif |
|
68 delta = (max_val - min_val) / n / 2; |
|
69 x = linspace (min_val+delta, max_val-delta, n); |
|
70 cutoff = x + delta; |
|
71 elseif (is_vector (x)) |
|
72 tmp = sort (x); |
|
73 if (any (tmp != x)) |
904
|
74 warning ("hist: bin values not sorted on input"); |
724
|
75 x = tmp; |
|
76 endif |
|
77 n = length (x); |
|
78 cutoff = zeros (1, n-1); |
|
79 for i = 1:n-1 |
|
80 cutoff (i) = (x (i) + x (i+1)) / 2; |
|
81 endfor |
|
82 else |
|
83 error ("hist: second argument must be a scalar or a vector"); |
|
84 endif |
|
85 endif |
|
86 |
|
87 freq = zeros (1, n); |
|
88 freq (1) = sum (y < cutoff (1)); |
|
89 for i = 2:n-1 |
|
90 freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i)); |
|
91 endfor |
|
92 freq (n) = sum (y >= cutoff (n-1)); |
|
93 |
3175
|
94 if (nargout > 0) |
736
|
95 nn = freq; |
|
96 xx = x; |
|
97 else |
|
98 bar (x, freq); |
724
|
99 endif |
|
100 |
|
101 endfunction |