Mercurial > octave
annotate scripts/statistics/histc.m @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | 7854d5752dd2 |
children | 597f3ee61a48 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2009-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
8932 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
8932 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
8932 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
8932 | 19 ## |
20 ## 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
|
21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
8932 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
27 ## @deftypefn {} {@var{n} =} histc (@var{x}, @var{edges}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
28 ## @deftypefnx {} {@var{n} =} histc (@var{x}, @var{edges}, @var{dim}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
29 ## @deftypefnx {} {[@var{n}, @var{idx}] =} histc (@dots{}) |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## Compute histogram counts. |
8932 | 31 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
32 ## When @var{x} is a vector, the function counts the number of elements of |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
33 ## @var{x} that fall in the histogram bins defined by @var{edges}. This |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
34 ## must be a vector of monotonically increasing values that define the edges |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
35 ## of the histogram bins. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
36 ## @tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
37 ## $n(k)$ |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
38 ## @end tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
39 ## @ifnottex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
40 ## @code{@var{n}(k)} |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
41 ## @end ifnottex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
42 ## contains the number of elements in @var{x} for which |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
43 ## @tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
44 ## $@var{edges}(k) <= @var{x} < @var{edges}(k+1)$. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
45 ## @end tex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
46 ## @ifnottex |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
47 ## @code{@var{edges}(k) <= @var{x} < @var{edges}(k+1)}. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
48 ## @end ifnottex |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
49 ## 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
|
50 ## exactly equal to the last element of @var{edges}. |
8932 | 51 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
52 ## When @var{x} is an @math{N}-dimensional array, the computation is carried |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
53 ## out along dimension @var{dim}. If not specified @var{dim} defaults to the |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
54 ## first non-singleton dimension. |
8932 | 55 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
56 ## When a second output argument is requested an index matrix is also returned. |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
57 ## The @var{idx} matrix has the same size as @var{x}. Each element of |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
58 ## @var{idx} contains the index of the histogram bin in which the |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
59 ## corresponding element of @var{x} was counted. |
8932 | 60 ## @seealso{hist} |
61 ## @end deftypefn | |
62 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
63 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
|
64 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
65 if (nargin < 2) |
8932 | 66 print_usage (); |
67 endif | |
68 | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
69 if (! isreal (x)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
70 error ("histc: X argument must be real-valued, not complex"); |
8932 | 71 endif |
72 | |
73 num_edges = numel (edges); | |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
74 if (num_edges == 0) |
23163
af64b6b61db9
histc.m: Return empty output when EDGES is empty for ML compatibility (bug #49944).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
75 warning ("histc: empty EDGES specified\n"); |
af64b6b61db9
histc.m: Return empty output when EDGES is empty for ML compatibility (bug #49944).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
76 n = idx = []; |
af64b6b61db9
histc.m: Return empty output when EDGES is empty for ML compatibility (bug #49944).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
77 return; |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
78 endif |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
79 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
80 if (! isreal (edges)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
81 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
|
82 else |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
83 ## Make sure 'edges' is sorted |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
84 edges = edges(:); |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
85 if (! issorted (edges) || edges(1) > edges(end)) |
8932 | 86 warning ("histc: edge values not sorted on input"); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
87 edges = sort (edges); |
8932 | 88 endif |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
89 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
90 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
91 nd = ndims (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
92 sz = size (x); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
93 if (nargin < 3) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
94 ## Find the first non-singleton dimension. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
95 (dim = find (sz > 1, 1)) || (dim = 1); |
8932 | 96 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
97 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
|
98 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
99 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
|
100 endif |
8932 | 101 endif |
102 | |
103 nsz = sz; | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
104 nsz(dim) = num_edges; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
105 |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
106 ## the splitting point is 3 bins |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
107 |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
108 if (num_edges <= 3) |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
109 |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
110 ## This is the O(M*N) algorithm. |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
111 |
8937
f27b2c95817f
slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents:
8935
diff
changeset
|
112 ## Allocate the histogram |
f27b2c95817f
slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents:
8935
diff
changeset
|
113 n = zeros (nsz); |
f27b2c95817f
slightly simplify histc
Jaroslav Hajek <highegg@gmail.com>
parents:
8935
diff
changeset
|
114 |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
115 ## Allocate 'idx' |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
116 if (nargout > 1) |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
117 idx = zeros (sz); |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
118 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
119 |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
120 ## Prepare indices |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
121 idx1 = cell (1, dim-1); |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
122 for k = 1:length (idx1) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
123 idx1{k} = 1:sz(k); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
124 endfor |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
125 idx2 = cell (length (sz) - dim); |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
126 for k = 1:length (idx2) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
127 idx2{k} = 1:sz(k+dim); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
128 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
129 |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
130 ## Compute the histograms |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
131 for k = 1:num_edges-1 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
132 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
|
133 n(idx1{:}, k, idx2{:}) = sum (b, dim); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
134 if (nargout > 1) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
135 idx(b) = k; |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
136 endif |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
137 endfor |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
138 b = (x == edges(end)); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
139 n(idx1{:}, num_edges, idx2{:}) = sum (b, dim); |
8932 | 140 if (nargout > 1) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
141 idx(b) = num_edges; |
8932 | 142 endif |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
143 |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
144 else |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
145 |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
146 ## This is the O(M*log(N) + N) algorithm. |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
147 |
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
148 ## Look-up indices. |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
149 idx = lookup (edges, x); |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
150 ## 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
|
151 idx(! (x <= edges(end))) = 0; |
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 iidx = idx; |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
154 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
155 ## 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
|
156 if (! isvector (x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
157 nl = prod (sz(1:dim-1)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
158 nn = sz(dim); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
159 nu = prod (sz(dim+1:end)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
160 if (nl != 1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
161 iidx = (iidx-1) * nl; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
162 iidx += reshape (kron (ones (1, nn*nu), 1:nl), sz); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
163 endif |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
164 if (nu != 1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
165 ne =length (edges); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
166 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
|
167 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
168 endif |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
169 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
170 ## Select valid elements. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
171 iidx = iidx(idx != 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
172 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
173 ## 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
|
174 n = accumarray (iidx(:), 1, nsz); |
8935
cae073411b03
optimize histc implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
8933
diff
changeset
|
175 |
8932 | 176 endif |
177 | |
178 endfunction | |
179 | |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
180 |
8932 | 181 %!test |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
182 %! x = linspace (0, 10, 1001); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
183 %! n = histc (x, 0:10); |
8932 | 184 %! assert (n, [repmat(100, 1, 10), 1]); |
185 | |
186 %!test | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
187 %! 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
|
188 %! n = histc (x, 0:10, 2); |
8932 | 189 %! assert (n, repmat ([repmat(100, 1, 10), 1], [2, 1, 3])); |
190 | |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
191 ## Test input validation |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
192 %!error <Invalid call> histc () |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
193 %!error <Invalid call> histc (1) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
194 %!error histc ([1:10 1+i], 2) |
23163
af64b6b61db9
histc.m: Return empty output when EDGES is empty for ML compatibility (bug #49944).
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
195 %!warning <empty EDGES specified> histc (1:10, []); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
196 %!error histc (1, 1, 3) |