annotate scripts/statistics/base/histc.m @ 19867:9fc020886ae9

maint: Clean up m-files to follow Octave coding conventions. Try to trim long lines to < 80 chars. Use '##' for single line comments. Use '(...)' around tests for if/elseif/switch/while. Abut cell indexing operator '{' next to variable. Abut array indexing operator '(' next to variable. Use space between negation operator '!' and following expression. Use two newlines between endfunction and start of %!test or %!demo code. Remove unnecessary parens grouping between short-circuit operators. Remove stray extra spaces (typos) between variables and assignment operators. Remove stray extra spaces from ends of lines.
author Rik <rik@octave.org>
date Mon, 23 Feb 2015 14:54:39 -0800
parents 4197fc428c7d
children d9341b422488
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 2009-2015 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
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
51 if (! isreal (x))
11436
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)
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
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
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
60 if (! isreal (edges))
10669
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
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
64 edges = edges(:);
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
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.
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
75 (dim = find (sz > 1, 1)) || (dim = 1);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
76 else
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
77 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
78 || !(1 <= dim && dim <= nd))
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
79 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
80 endif
8932
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 nsz = sz;
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
84 nsz(dim) = num_edges;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
85
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
86 ## the splitting point is 3 bins
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
87
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
88 if (num_edges <= 3)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
89
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
90 ## This is the O(M*N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
91
8937
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
92 ## Allocate the histogram
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
93 n = zeros (nsz);
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
94
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
95 ## Allocate 'idx'
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
96 if (nargout > 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
97 idx = zeros (sz);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
98 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
99
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
100 ## Prepare indices
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
101 idx1 = cell (1, dim-1);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
102 for k = 1:length (idx1)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
103 idx1{k} = 1:sz(k);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
104 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
105 idx2 = cell (length (sz) - dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
106 for k = 1:length (idx2)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
107 idx2{k} = 1:sz(k+dim);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
108 endfor
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
109
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
110 ## Compute the histograms
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
111 for k = 1:num_edges-1
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
112 b = (edges(k) <= x & x < edges(k+1));
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
113 n(idx1{:}, k, idx2{:}) = sum (b, dim);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
114 if (nargout > 1)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
115 idx(b) = k;
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
116 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
117 endfor
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
118 b = (x == edges(end));
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
119 n(idx1{:}, num_edges, idx2{:}) = sum (b, dim);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
120 if (nargout > 1)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
121 idx(b) = num_edges;
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
122 endif
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
123
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
124 else
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
125
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
126 ## This is the O(M*log(N) + N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
127
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
128 ## Look-up indices.
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
129 idx = lookup (edges, x);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
130 ## Zero invalid ones (including NaNs). x < edges(1) are already zero.
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
131 idx(! (x <= edges(end))) = 0;
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
132
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
133 iidx = idx;
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
134
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
135 ## 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
136 if (! isvector (x))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
137 nl = prod (sz(1:dim-1));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
138 nn = sz(dim);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
139 nu = prod (sz(dim+1:end));
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
140 if (nl != 1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
141 iidx = (iidx-1) * nl;
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
142 iidx += reshape (kron (ones (1, nn*nu), 1:nl), sz);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
143 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
144 if (nu != 1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
145 ne =length (edges);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
146 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
147 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
148 endif
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
149
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
150 ## Select valid elements.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
151 iidx = iidx(idx != 0);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
152
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
153 ## 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
154 n = accumarray (iidx(:), 1, nsz);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
155
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
156 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
157
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
158 endfunction
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
159
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
160
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
161 %!test
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
162 %! x = linspace (0, 10, 1001);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
163 %! n = histc (x, 0:10);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
164 %! assert (n, [repmat(100, 1, 10), 1]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
165
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
166 %!test
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
167 %! 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
168 %! n = histc (x, 0:10, 2);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
169 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3]));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
170
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
171 %!error histc ()
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
172 %!error histc (1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
173 %!error histc (1, 2, 3, 4)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
174 %!error histc ([1:10 1+i], 2)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
175 %!error histc (1:10, [])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
176 %!error histc (1, 1, 3)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
177