annotate scripts/statistics/base/histc.m @ 10687:a8ce6bdecce5

Improve documentation strings.
author Rik <octave@nomad.inbox5.com>
date Tue, 08 Jun 2010 20:22:38 -0700
parents cab3b148d4e4
children be55736a0783
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 -*-
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{n} =} histc (@var{y}, @var{edges})
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
22 ## @deftypefnx {Function File} {@var{n} =} histc (@var{y}, @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 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
26 ## When @var{y} is a vector, the function counts the number of elements of
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8937
diff changeset
27 ## @var{y} that fall in the histogram bins defined by @var{edges}. This must be
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
28 ## a vector of monotonically non-decreasing values that define the edges of the
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8937
diff changeset
29 ## histogram bins. So, @code{@var{n} (k)} contains the number of elements in
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
30 ## @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
31 ## 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
32 ## that was equal to the last element of @var{edges}.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
33 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
34 ## When @var{y} is a @math{N}-dimensional array, the same operation as above is
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
35 ## repeated along dimension @var{dim}. If not specified @var{dim} defaults
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 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
38 ## If a second output argument is requested an index matrix is also returned.
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8937
diff changeset
39 ## The @var{idx} matrix has same size as @var{y}. 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
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
41 ## of @var{y} was counted.
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
42 ##
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
43 ## @seealso{hist}
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
44 ## @end deftypefn
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
45
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
46 function [n, idx] = histc (data, edges, dim)
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
47 ## Check input
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
48 if (nargin < 2 || nargin > 3)
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
49 print_usage ();
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
50 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
51
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
52 if (!isreal (data))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
53 error ("histc: Y argument must be real-valued, not complex");
8932
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 num_edges = numel (edges);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
57 if (num_edges == 0)
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
58 error ("histc: EDGES must not be empty")
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
59 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
60
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
61 if (!isreal (edges))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
62 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
63 else
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
64 ## Make sure 'edges' is sorted
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
65 edges = edges (:);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
66 if (! issorted (edges) || edges(1) > edges(end))
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
67 warning ("histc: edge values not sorted on input");
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
68 edges = sort (edges);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
69 endif
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
70 endif
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
71
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
72 nd = ndims (data);
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
73 sz = size (data);
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
74 if (nargin < 3)
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
75 ## 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
76 dim = find (sz > 1, 1);
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
77 if (isempty (dim))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
78 dim = 1;
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
79 endif
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
80 else
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
81 if (!(isscalar (dim) && dim == round (dim)) ||
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
82 !(1 <= dim && dim <= nd))
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 9051
diff changeset
83 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
84 endif
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
85 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
86
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
87 nsz = sz;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
88 nsz (dim) = num_edges;
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
89
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
90 ## the splitting point is 3 bins
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
91
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
92 if (num_edges <= 3)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
93
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
94 ## This is the O(M*N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
95
8937
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
96 ## Allocate the histogram
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
97 n = zeros (nsz);
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
98
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
99 ## Allocate 'idx'
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
100 if (nargout > 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
101 idx = zeros (sz);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
102 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
103
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
104 ## Prepare indices
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
105 idx1 = cell (1, dim-1);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
106 for k = 1:length (idx1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
107 idx1 {k} = 1:sz (k);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
108 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
109 idx2 = cell (length (sz) - dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
110 for k = 1:length (idx2)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
111 idx2 {k} = 1:sz (k+dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
112 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
113
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
114 ## Compute the histograms
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
115 for k = 1:num_edges-1
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
116 b = (edges (k) <= data & data < edges (k+1));
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
117 n (idx1 {:}, k, idx2 {:}) = sum (b, dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
118 if (nargout > 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
119 idx (b) = k;
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
120 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
121 endfor
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
122 b = (data == edges (end));
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
123 n (idx1 {:}, num_edges, idx2 {:}) = sum (b, dim);
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
124 if (nargout > 1)
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
125 idx (b) = num_edges;
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
126 endif
8935
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 else
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
129
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
130 ## This is the O(M*log(N) + N) algorithm.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
131
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
132 ## Look-up indices.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
133 idx = lookup (edges, data);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
134 ## Zero invalid ones (including NaNs). data < edges(1) are already zero.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
135 idx(! (data <= edges(end))) = 0;
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
136
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
137 iidx = idx;
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
138
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
139 ## In case of matrix input, we adjust the indices.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
140 if (! isvector (data))
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
141 nl = prod (sz(1:dim-1));
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
142 nn = sz(dim);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
143 nu = prod (sz(dim+1:end));
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
144 if (nl != 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
145 iidx = (iidx-1) * nl;
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
146 iidx += reshape (kron (ones (1, nn*nu), 1:nl), sz);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
147 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
148 if (nu != 1)
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
149 ne =length (edges);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
150 iidx += reshape (kron (nl*ne*(0:nu-1), ones (1, nl*nn)), sz);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
151 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
152 endif
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
153
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
154 ## Select valid elements.
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
155 iidx = iidx(idx != 0);
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
156
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
157 ## Call accumarray to sum the indexed elements.
8937
f27b2c95817f slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents: 8935
diff changeset
158 n = accumarray (iidx(:), 1, nsz);
8935
cae073411b03 optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents: 8933
diff changeset
159
8932
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
160 endif
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
161
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
162 endfunction
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
163
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
164 %!test
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
165 %! data = linspace (0, 10, 1001);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
166 %! n = histc (data, 0:10);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
167 %! assert (n, [repmat(100, 1, 10), 1]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
168
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
169 %!test
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
170 %! data = repmat (linspace (0, 10, 1001), [2, 1, 3]);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
171 %! n = histc (data, 0:10, 2);
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
172 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3]));
2d0f8692a82e Add the 'histc' function
Soren Hauberg <hauberg@gmail.com>
parents:
diff changeset
173