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