Mercurial > hg > octave-lyh
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 |
rev | line source |
---|---|
8932 | 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 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
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 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
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 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{n} =} histc (@var{y}, @var{edges}) | |
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 | 24 ## Produce histogram counts. |
25 ## | |
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 | 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 | 30 ## @var{y} for which @code{@var{edges} (k) <= @var{y} < @var{edges} (k+1)}. |
31 ## The final element of @var{n} contains the number of elements of @var{y} | |
32 ## that was equal to the last element of @var{edges}. | |
33 ## | |
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 | 37 ## |
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 | 40 ## contains the index of the histogram bin in which the corresponding element |
41 ## of @var{y} was counted. | |
42 ## | |
43 ## @seealso{hist} | |
44 ## @end deftypefn | |
45 | |
46 function [n, idx] = histc (data, edges, dim) | |
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 | 49 print_usage (); |
50 endif | |
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 | 54 endif |
55 | |
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 | 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 | 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 | 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 | 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 | 85 endif |
86 | |
87 nsz = sz; | |
88 nsz (dim) = num_edges; | |
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 | 124 if (nargout > 1) |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
125 idx (b) = num_edges; |
8932 | 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 | 160 endif |
161 | |
162 endfunction | |
163 | |
164 %!test | |
165 %! data = linspace (0, 10, 1001); | |
166 %! n = histc (data, 0:10); | |
167 %! assert (n, [repmat(100, 1, 10), 1]); | |
168 | |
169 %!test | |
170 %! data = repmat (linspace (0, 10, 1001), [2, 1, 3]); | |
171 %! n = histc (data, 0:10, 2); | |
172 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3])); | |
173 |