annotate scripts/statistics/base/histc.m @ 8933:346fde2030b5

scripts/statistics/base/histc.m: update copyright notice to match the rest of Octave
author Soren Hauberg <hauberg@gmail.com>
date Sun, 08 Mar 2009 19:18:44 +0100
parents 2d0f8692a82e
children cae073411b03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
1 ## Copyright (C) 2009, Søren Hauberg
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
2 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
4 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
8933
346fde2030b5 scripts/statistics/base/histc.m: update copyright notice to match the rest of Octave
Soren Hauberg <hauberg@gmail.com>
parents: 8932
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
346fde2030b5 scripts/statistics/base/histc.m: update copyright notice to match the rest of Octave
Soren Hauberg <hauberg@gmail.com>
parents: 8932
diff changeset
8 ## your option) any later version.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
9 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
14 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
8933
346fde2030b5 scripts/statistics/base/histc.m: update copyright notice to match the rest of Octave
Soren Hauberg <hauberg@gmail.com>
parents: 8932
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
346fde2030b5 scripts/statistics/base/histc.m: update copyright notice to match the rest of Octave
Soren Hauberg <hauberg@gmail.com>
parents: 8932
diff changeset
17 ## <http://www.gnu.org/licenses/>.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
18
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{n} =} histc (@var{y}, @var{edges})
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{n} =} histc (@var{y}, @var{edges}, @var{dim})
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
22 ## @deftypefnx {Function File} {[@var{n}, @var{idx}] =} histc (...)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
23 ## Produce histogram counts.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
24 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
25 ## When @var{y} is a vector, the function counts the number of elements of
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
26 ## @var{y} that fall in the histogram bins defined by @var{edges}. This must be
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
27 ## a vector of monotonically non-decreasing values that define the edges of the
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
28 ## histogram bins. So, @code{@var{n} (k)} contains the number of elements in
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
29 ## @var{y} for which @code{@var{edges} (k) <= @var{y} < @var{edges} (k+1)}.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
30 ## The final element of @var{n} contains the number of elements of @var{y}
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
31 ## that was equal to the last element of @var{edges}.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
32 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
33 ## When @var{y} is a @math{N}-dimensional array, the same operation as above is
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
34 ## repeated along dimension @var{dim}. If this argument is given, the operation
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
35 ## is performed along the first non-singleton dimension.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
36 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
37 ## If a second output argument is requested an index matrix is also returned.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
38 ## The @var{idx} matrix has same size as @var{y}. Each element of @var{idx}
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
39 ## contains the index of the histogram bin in which the corresponding element
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
40 ## of @var{y} was counted.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
41 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
42 ## @seealso{hist}
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
43 ## @end deftypefn
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
44
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
45 function [n, idx] = histc (data, edges, dim)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
46 ## Check input
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
47 if (nargin < 2)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
48 print_usage ();
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
49 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
50
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
51 sz = size (data);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
52 if (nargin < 3)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
53 dim = find (sz > 1, 1);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
54 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
55
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
56 if (!isreal (data))
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
57 error ("histc: first argument must be real a vector");
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
58 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
59
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
60 ## Make sure 'edges' is sorted
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
61 num_edges = numel (edges);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
62 if (isreal (edges))
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
63 edges = edges (:);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
64 tmp = sort (edges);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
65 if (any (tmp != edges))
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
66 warning ("histc: edge values not sorted on input");
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
67 edges = tmp;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
68 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
69 else
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
70 error ("histc: second argument must be a vector");
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
71 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
72
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
73 ## Allocate the histogram
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
74 nsz = sz;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
75 nsz (dim) = num_edges;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
76 n = zeros (nsz);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
77
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
78 ## Allocate 'idx'
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
79 if (nargout > 1)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
80 idx = zeros (sz);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
81 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
82
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
83 ## Prepare indices
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
84 idx1 = cell (1, dim-1);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
85 for k = 1:length (idx1)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
86 idx1 {k} = 1:sz (k);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
87 endfor
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
88 idx2 = cell (length (sz) - dim);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
89 for k = 1:length (idx2)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
90 idx2 {k} = 1:sz (k+dim);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
91 endfor
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
92
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
93 ## Compute the histograms
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
94 for k = 1:num_edges-1
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
95 b = (edges (k) <= data & data < edges (k+1));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
96 n (idx1 {:}, k, idx2 {:}) = sum (b, dim);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
97 if (nargout > 1)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
98 idx (b) = k;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
99 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
100 endfor
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
101 b = (data == edges (end));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
102 n (idx1 {:}, num_edges, idx2 {:}) = sum (b, dim);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
103 if (nargout > 1)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
104 idx (b) = num_edges;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
105 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
106
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
107 endfunction
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
108
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
109 %!test
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
110 %! data = linspace (0, 10, 1001);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
111 %! n = histc (data, 0:10);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
112 %! assert (n, [repmat(100, 1, 10), 1]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
113
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
114 %!test
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
115 %! data = repmat (linspace (0, 10, 1001), [2, 1, 3]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
116 %! n = histc (data, 0:10, 2);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
117 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3]));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
118