724
|
1 # Copyright (C) 1994 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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # 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, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
18 |
|
19 function [nn, xx] = hist (y, x) |
|
20 |
|
21 # usage: [NN, XX] = hist (Y, X) or hist (Y, X) |
|
22 # |
|
23 # Produce histogram counts or plots. |
|
24 # |
|
25 # With one vector input argument, plot a histogram of the values with |
|
26 # 10 bins. The range of the histogram bins is determined by the range |
|
27 # of the data. |
|
28 # |
|
29 # Given a second scalar argument, use that as the number of bins. |
|
30 # |
|
31 # Given a second vector argument, use that as the centers of the bins, |
|
32 # with the width of the bins determened from the adjacent values in |
|
33 # the vector. |
|
34 # |
|
35 # Extreme values are lumped in the first and last bins. |
|
36 # |
|
37 # With two output arguments, produce the values NN and XX such that |
|
38 # bar (XX, NN) will plot the histogram. |
|
39 # |
|
40 # See also: bar |
|
41 |
|
42 if (nargin < 1 || nargin > 2) |
|
43 error ("usage: [nn, xx] = hist (y, x)"); |
|
44 endif |
|
45 |
|
46 if (is_vector (y)) |
|
47 max_val = max (y); |
|
48 min_val = min (y); |
|
49 else |
|
50 error ("hist: first argument must be a vector"); |
|
51 endif |
|
52 |
|
53 if (nargin == 1) |
|
54 n = 10; |
|
55 delta = (max_val - min_val) / n / 2; |
|
56 x = linspace (min_val+delta, max_val-delta, n); |
|
57 cutoff = x + delta; |
|
58 elseif (nargin == 2) |
|
59 if (is_scalar (x)) |
|
60 n = x; |
|
61 if (n <= 0) |
|
62 error ("hist: number of bins must be positive"); |
|
63 endif |
|
64 delta = (max_val - min_val) / n / 2; |
|
65 x = linspace (min_val+delta, max_val-delta, n); |
|
66 cutoff = x + delta; |
|
67 elseif (is_vector (x)) |
|
68 tmp = sort (x); |
|
69 if (any (tmp != x)) |
|
70 fprintf (stderr, "warning: hist: bin values not sorted on input\n"); |
|
71 x = tmp; |
|
72 endif |
|
73 n = length (x); |
|
74 cutoff = zeros (1, n-1); |
|
75 for i = 1:n-1 |
|
76 cutoff (i) = (x (i) + x (i+1)) / 2; |
|
77 endfor |
|
78 else |
|
79 error ("hist: second argument must be a scalar or a vector"); |
|
80 endif |
|
81 endif |
|
82 |
|
83 freq = zeros (1, n); |
|
84 freq (1) = sum (y < cutoff (1)); |
|
85 for i = 2:n-1 |
|
86 freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i)); |
|
87 endfor |
|
88 freq (n) = sum (y >= cutoff (n-1)); |
|
89 |
|
90 # With Octave 1.0, nargout is never zero, so we have to do this: |
|
91 |
|
92 nn = freq; |
|
93 xx = x; |
|
94 if (nargout != 2) |
|
95 bar (xx, nn); |
|
96 endif |
|
97 |
|
98 # Once 1.1 is released, we can do this instead: |
|
99 # |
|
100 # if (nargout == 2) |
|
101 # nn = freq; |
|
102 # xx = x; |
|
103 # else |
|
104 # bar (x, freq); |
|
105 # endif |
|
106 |
|
107 endfunction |