annotate scripts/general/accumdim.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 01de0045b2e3
children 5d3faba0342e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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: 30330
diff changeset
3 ## Copyright (C) 2010-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/>.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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: 23220
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.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
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.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
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: 23220
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 ########################################################################
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @deftypefn {} {} accumdim (@var{subs}, @var{vals}, @var{dim}, @var{n}, @var{func}, @var{fillval})
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 ## Create an array by accumulating the slices of an array into the
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
29 ## positions defined by their subscripts along a specified dimension.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ##
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31 ## The subscripts are defined by the index vector @var{subs}.
10711
fbd7843974fa Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents: 10692
diff changeset
32 ## The dimension is specified by @var{dim}. If not given, it defaults
14119
94e2a76f1e5a doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14116
diff changeset
33 ## to the first non-singleton dimension. The length of @var{subs} must
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
34 ## be equal to @code{size (@var{vals}, @var{dim})}.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 ##
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
36 ## The extent of the result matrix in the working dimension will be
14119
94e2a76f1e5a doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14116
diff changeset
37 ## determined by the subscripts themselves. However, if @var{n} is
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
38 ## defined it determines this extent.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40 ## The default action of @code{accumdim} is to sum the subarrays with the
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
41 ## same subscripts. This behavior can be modified by defining the
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
42 ## @var{func} function. This should be a function or function handle
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
43 ## that accepts an array and a dimension, and reduces the array along
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
44 ## this dimension. As a special exception, the built-in @code{min} and
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
45 ## @code{max} functions can be used directly, and @code{accumdim}
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
46 ## accounts for the middle empty argument that is used in their calling.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
47 ##
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
48 ## The slices of the returned array that have no subscripts associated
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
49 ## with them are set to zero. Defining @var{fillval} to some other
14119
94e2a76f1e5a doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 14116
diff changeset
50 ## value allows these values to be defined.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 ##
10397
180931276a52 fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents: 10396
diff changeset
52 ## An example of the use of @code{accumdim} is:
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 ##
14116
951eacaf9381 Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14113
diff changeset
54 ## @example
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 ## @group
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
56 ## accumdim ([1, 2, 1, 2, 1], [ 7, -10, 4;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
57 ## -5, -12, 8;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
58 ## -12, 2, 8;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
59 ## -10, 9, -3;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
60 ## -5, -3, -13])
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
61 ## @result{} [-10,-11,-1;-15,-3,5]
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 ## @end group
14116
951eacaf9381 Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14113
diff changeset
63 ## @end example
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 ## @seealso{accumarray}
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 ## @end deftypefn
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
68 function A = accumdim (subs, vals, dim, n = 0, func = [], fillval = 0)
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27923
diff changeset
70 if (nargin < 2)
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 print_usage ();
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 if (isempty (fillval))
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 fillval = 0;
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 if (! isvector (subs))
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 10397
diff changeset
79 error ("accumdim: SUBS must be a subscript vector");
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80 elseif (! isindex (subs)) # creates index cache
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 error ("accumdim: indices must be positive integers");
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 else
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 m = max (subs);
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
84 if (n == 0 || isempty (n))
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85 n = m;
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86 elseif (n < m)
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
87 error ("accumdim: N index out of range");
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
88 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
90
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
91 sz = size (vals);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 if (nargin < 3)
14221
b03b15d507d4 accumdim.m: Use common code idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
94 ## Find the first non-singleton dimension.
b03b15d507d4 accumdim.m: Use common code idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
95 (dim = find (sz > 1, 1)) || (dim = 1);
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
96 elseif (! isindex (dim))
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 10397
diff changeset
97 error ("accumdim: DIM must be a valid dimension");
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
98 elseif (dim > length (sz))
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
99 sz(end+1:dim) = 1;
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 sz(dim) = n;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
102
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
103 if (length (subs) != size (vals, dim))
15202
f3b5cadfd6d5 fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
104 error ("accumdim: dimension mismatch");
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
105 endif
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
106
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
107 if (isempty (func) || func == @sum)
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
108 ## Fast summation case.
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
109 A = __accumdim_sum__ (subs, vals, dim, n);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
110
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
111 ## Fill in nonzero fill value
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
112 if (fillval != 0)
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
113 mask = true (n, 1);
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
114 mask(subs) = false;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
115 subsc = {':'}(ones (1, length (sz)));
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
116 subsc{dim} = mask;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
117 A(subsc{:}) = fillval;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
118 endif
17312
088d014a7fe2 Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents: 16777
diff changeset
119 return;
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
120 endif
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
121
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
122 ## The general case.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 ns = length (subs);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 ## Sort indices.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 [subs, idx] = sort (subs(:));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 ## Identify runs.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 jdx = find (subs(1:ns-1) != subs(2:ns));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 jdx = [jdx; ns];
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 ## Collect common slices.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130 szc = num2cell (sz);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131 szc{dim} = diff ([0; jdx]);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 subsc = {':'}(ones (1, length (sz)));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 subsc{dim} = idx;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
134 vals = mat2cell (vals(subsc{:}), szc{:});
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 20852
diff changeset
135 ## Apply reductions. Special case min, max.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 if (func == @min || func == @max)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
137 vals = cellfun (func, vals, {[]}, {dim}, "uniformoutput", false);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
139 vals = cellfun (func, vals, {dim}, "uniformoutput", false);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
140 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 subs = subs(jdx);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 ## Concatenate reduced slices.
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
144 vals = cat (dim, vals{:});
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 ## Construct matrix of fillvals.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147 if (fillval == 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
148 A = zeros (sz, class (vals));
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 else
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 A = repmat (fillval, sz);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
153 ## Set the reduced values.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 subsc{dim} = subs;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
155 A(subsc{:}) = vals;
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
156
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 endfunction
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
159
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
160 ## Test accumdim vs. accumarray
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 %!shared a
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
162 %! a = rand (5, 5, 5);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
163
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
164 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3)))
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
165 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
166 %! accumarray ([2;3;2;2;2], a(4,:,2), [1,4]))
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
167 %!assert (accumdim ([2;3;2;1;2], a, 3, 3, @min)(1,5,:),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
168 %! accumarray ([2;3;2;1;2], a(1,5,:), [1,1,3], @min))
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
169 %!assert (accumdim ([1;3;2;2;1], a, 2, 3, @median)(4,:,5),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
170 %! accumarray ([1;3;2;2;1], a(4,:,5), [1,3], @median))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
171
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
172 ## Test fillval
16777
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
173 %!assert (accumdim ([1;3;1;3;3], a)(2,:,:), zeros (1,5,5))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
174 %!assert (accumdim ([1;3;1;3;3], a, 1, 4)([2 4],:,:), zeros (2,5,5))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
175 %!assert (accumdim ([1;3;1;3;3], a, 1, 4, [], pi)([2 4],:,:), pi (2,5,5))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
176
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
177 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
178 %!error <Invalid call> accumdim ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
179 %!error <Invalid call> accumdim (1)
16777
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
180 %!error <SUBS must be a subscript vector> accumdim (ones (2,2), ones (2,2))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
181 %!error <indices must be positive integers> accumdim ([-1 1], ones (2,2))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
182 %!error <N index out of range> accumdim ([1 2], ones (2,2), 1, 1)
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
183 %!error <dimension mismatch> accumdim ([1], ones (2,2))