annotate scripts/statistics/base/histc.m @ 11436:e151e23f73bc

Overhaul base statistics functions and documentation of same. Add or improve input validation. Add input validation tests. Add functional tests. Improve or re-write documentation strings.
author Rik <octave@nomad.inbox5.com>
date Mon, 03 Jan 2011 21:23:08 -0800
parents fe3c3dfc07eb
children fd0a3ac60b0e
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
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
3 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
4 ## This file is part of Octave.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
5 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
10 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
14 ## General Public License for more details.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
15 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
16 ## 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
17 ## 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
18 ## <http://www.gnu.org/licenses/>.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
19
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
20 ## -*- texinfo -*-
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
21 ## @deftypefn {Function File} {@var{n} =} histc (@var{x}, @var{edges})
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
22 ## @deftypefnx {Function File} {@var{n} =} histc (@var{x}, @var{edges}, @var{dim})
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8937
diff changeset
23 ## @deftypefnx {Function File} {[@var{n}, @var{idx}] =} histc (@dots{})
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
24 ## Produce histogram counts.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
25 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
26 ## When @var{x} is a vector, the function counts the number of elements of
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
27 ## @var{x} that fall in the histogram bins defined by @var{edges}. This must be
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
28 ## a vector of monotonically increasing values that define the edges of the
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
29 ## histogram bins. @code{@var{n}(k)} contains the number of elements in
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
30 ## @var{x} for which @code{@var{edges}(k) <= @var{x} < @var{edges}(k+1)}.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
31 ## The final element of @var{n} contains the number of elements of @var{x}
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
32 ## exactly equal to the last element of @var{edges}.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
33 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
34 ## When @var{x} is an @math{N}-dimensional array, the computation is
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
35 ## carried out along dimension @var{dim}. If not specified @var{dim} defaults
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
36 ## to the first non-singleton dimension.
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
37 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
38 ## When a second output argument is requested an index matrix is also returned.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
39 ## The @var{idx} matrix has the same size as @var{x}. Each element of @var{idx}
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
40 ## contains the index of the histogram bin in which the corresponding element
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
41 ## of @var{x} was counted.
8932
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
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
45 function [n, idx] = histc (x, edges, dim)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
46
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
47 if (nargin < 2 || nargin > 3)
8932
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
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
51 if (!isreal (x))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
52 error ("histc: X argument must be real-valued, not complex");
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
53 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
54
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
55 num_edges = numel (edges);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
56 if (num_edges == 0)
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
57 error ("histc: EDGES must not be empty")
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
58 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
59
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
60 if (!isreal (edges))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
61 error ("histc: EDGES must be real-valued, not complex");
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
62 else
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
63 ## Make sure 'edges' is sorted
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
64 edges = edges (:);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
65 if (!issorted (edges) || edges(1) > edges(end))
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
66 warning ("histc: edge values not sorted on input");
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
67 edges = sort (edges);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
68 endif
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
69 endif
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
70
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
71 nd = ndims (x);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
72 sz = size (x);
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
73 if (nargin < 3)
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
74 ## Find the first non-singleton dimension.
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
75 dim = find (sz > 1, 1);
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
76 if (isempty (dim))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
77 dim = 1;
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
78 endif
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
79 else
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
80 if (!(isscalar (dim) && dim == fix (dim))
11149
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10851
diff changeset
81 || !(1 <= dim && dim <= nd))
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
82 error ("histc: DIM must be an integer and a valid dimension");
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
83 endif
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
84 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
85
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
86 nsz = sz;
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
87 nsz(dim) = num_edges;
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
88
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
89 ## the splitting point is 3 bins
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
90
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
91 if (num_edges <= 3)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
92
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
93 ## This is the O(M*N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
94
8937
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
95 ## Allocate the histogram
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
96 n = zeros (nsz);
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
97
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
98 ## Allocate 'idx'
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
99 if (nargout > 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
100 idx = zeros (sz);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
101 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
102
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
103 ## Prepare indices
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
104 idx1 = cell (1, dim-1);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
105 for k = 1:length (idx1)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
106 idx1 {k} = 1:sz(k);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
107 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
108 idx2 = cell (length (sz) - dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
109 for k = 1:length (idx2)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
110 idx2 {k} = 1:sz(k+dim);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
111 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
112
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
113 ## Compute the histograms
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
114 for k = 1:num_edges-1
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
115 b = (edges (k) <= x & x < edges (k+1));
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
116 n (idx1 {:}, k, idx2 {:}) = sum (b, dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
117 if (nargout > 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
118 idx (b) = k;
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
119 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
120 endfor
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
121 b = (x == edges (end));
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
122 n (idx1 {:}, num_edges, idx2 {:}) = sum (b, dim);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
123 if (nargout > 1)
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
124 idx (b) = num_edges;
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
125 endif
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
126
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
127 else
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
128
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
129 ## This is the O(M*log(N) + N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
130
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
131 ## Look-up indices.
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
132 idx = lookup (edges, x);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
133 ## Zero invalid ones (including NaNs). x < edges(1) are already zero.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
134 idx(! (x <= edges(end))) = 0;
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
135
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
136 iidx = idx;
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
137
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
138 ## In case of matrix input, we adjust the indices.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
139 if (! isvector (x))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
140 nl = prod (sz(1:dim-1));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
141 nn = sz(dim);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
142 nu = prod (sz(dim+1:end));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
143 if (nl != 1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
144 iidx = (iidx-1) * nl;
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
145 iidx += reshape (kron (ones (1, nn*nu), 1:nl), sz);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
146 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
147 if (nu != 1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
148 ne =length (edges);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
149 iidx += reshape (kron (nl*ne*(0:nu-1), ones (1, nl*nn)), sz);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
150 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
151 endif
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
152
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
153 ## Select valid elements.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
154 iidx = iidx(idx != 0);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
155
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
156 ## Call accumarray to sum the indexed elements.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
157 n = accumarray (iidx(:), 1, nsz);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
158
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
159 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
160
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
161 endfunction
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
162
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
163 %!test
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
164 %! x = linspace (0, 10, 1001);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
165 %! n = histc (x, 0:10);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
166 %! assert (n, [repmat(100, 1, 10), 1]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
167
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
168 %!test
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
169 %! x = repmat (linspace (0, 10, 1001), [2, 1, 3]);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
170 %! n = histc (x, 0:10, 2);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
171 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3]));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
172
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
173 %!error histc ();
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
174 %!error histc (1);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
175 %!error histc (1, 2, 3, 4);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
176 %!error histc ([1:10 1+i], 2);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
177 %!error histc (1:10, []);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
178 %!error histc (1, 1, 3);