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