annotate scripts/general/accumdim.m @ 20158:7503499a252b stable

doc: Update docstrings to have one sentence summary as first line. Update scripts in audio, elfun, general, geometry, and image directories. * scripts/audio/@audioplayer/__get_properties__.m, scripts/audio/@audioplayer/audioplayer.m, scripts/audio/@audioplayer/get.m, scripts/audio/@audioplayer/isplaying.m, scripts/audio/@audioplayer/play.m, scripts/audio/@audioplayer/playblocking.m, scripts/audio/@audioplayer/set.m, scripts/audio/@audioplayer/subsasgn.m, scripts/audio/@audioplayer/subsref.m, scripts/audio/@audiorecorder/audiorecorder.m, scripts/audio/@audiorecorder/get.m, scripts/audio/@audiorecorder/getaudiodata.m, scripts/audio/@audiorecorder/getplayer.m, scripts/audio/@audiorecorder/isrecording.m, scripts/audio/@audiorecorder/play.m, scripts/audio/@audiorecorder/record.m, scripts/audio/@audiorecorder/recordblocking.m, scripts/audio/@audiorecorder/set.m, scripts/audio/@audiorecorder/stop.m, scripts/audio/@audiorecorder/subsasgn.m, scripts/audio/@audiorecorder/subsref.m, scripts/audio/lin2mu.m, scripts/audio/mu2lin.m, scripts/audio/record.m, scripts/audio/sound.m, scripts/audio/soundsc.m, scripts/audio/wavread.m, scripts/audio/wavwrite.m, scripts/elfun/cosd.m, scripts/elfun/sind.m, scripts/elfun/tand.m, scripts/general/accumarray.m, scripts/general/accumdim.m, scripts/general/bitcmp.m, scripts/general/bitget.m, scripts/general/bitset.m, scripts/general/blkdiag.m, scripts/general/cart2pol.m, scripts/general/cart2sph.m, scripts/general/cell2mat.m, scripts/general/celldisp.m, scripts/general/chop.m, scripts/general/circshift.m, scripts/general/common_size.m, scripts/general/cplxpair.m, scripts/general/cumtrapz.m, scripts/general/dblquad.m, scripts/general/deal.m, scripts/general/del2.m, scripts/general/display.m, scripts/general/divergence.m, scripts/general/fieldnames.m, scripts/general/flip.m, scripts/general/flipdim.m, scripts/general/fliplr.m, scripts/general/flipud.m, scripts/general/gradient.m, scripts/general/interp3.m, scripts/general/interpft.m, scripts/general/interpn.m, scripts/general/loadobj.m, scripts/general/logspace.m, scripts/general/methods.m, scripts/general/nargchk.m, scripts/general/narginchk.m, scripts/general/nargoutchk.m, scripts/general/nextpow2.m, scripts/general/nthargout.m, scripts/general/num2str.m, scripts/general/pol2cart.m, scripts/general/polyarea.m, scripts/general/postpad.m, scripts/general/prepad.m, scripts/general/profile.m, scripts/general/quadgk.m, scripts/general/quadl.m, scripts/general/quadv.m, scripts/general/randi.m, scripts/general/rat.m, scripts/general/repmat.m, scripts/general/rot90.m, scripts/general/rotdim.m, scripts/general/saveobj.m, scripts/general/shift.m, scripts/general/shiftdim.m, scripts/general/sortrows.m, scripts/general/sph2cart.m, scripts/general/structfun.m, scripts/general/subsindex.m, scripts/general/trapz.m, scripts/general/triplequad.m, scripts/geometry/delaunayn.m, scripts/geometry/dsearch.m, scripts/geometry/dsearchn.m, scripts/geometry/griddata.m, scripts/geometry/griddata3.m, scripts/geometry/griddatan.m, scripts/geometry/inpolygon.m, scripts/geometry/rectint.m, scripts/geometry/tsearchn.m, scripts/geometry/voronoi.m, scripts/geometry/voronoin.m, scripts/help/__unimplemented__.m, scripts/help/doc.m, scripts/help/doc_cache_create.m, scripts/help/get_first_help_sentence.m, scripts/help/help.m, scripts/help/lookfor.m, scripts/help/print_usage.m, scripts/help/type.m, scripts/help/which.m, scripts/image/autumn.m, scripts/image/bone.m, scripts/image/brighten.m, scripts/image/cmpermute.m, scripts/image/colorcube.m, scripts/image/contrast.m, scripts/image/cool.m, scripts/image/copper.m, scripts/image/cubehelix.m, scripts/image/flag.m, scripts/image/gmap40.m, scripts/image/gray.m, scripts/image/gray2ind.m, scripts/image/hot.m, scripts/image/hsv.m, scripts/image/image.m, scripts/image/imagesc.m, scripts/image/imfinfo.m, scripts/image/imformats.m, scripts/image/imread.m, scripts/image/imshow.m, scripts/image/imwrite.m, scripts/image/iscolormap.m, scripts/image/jet.m, scripts/image/lines.m, scripts/image/ntsc2rgb.m, scripts/image/ocean.m, scripts/image/pink.m, scripts/image/prism.m, scripts/image/rainbow.m, scripts/image/rgb2ntsc.m, scripts/image/spinmap.m, scripts/image/spring.m, scripts/image/summer.m, scripts/image/white.m, scripts/image/winter.m: Update docstrings to have one sentence summary as first line. Re-structure to have line lengths <= 80 chars.
author Rik <rik@octave.org>
date Sun, 03 May 2015 09:36:20 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 2010-2015 VZLU Prague
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
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 ## You should have received a copy of the GNU General Public License
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 10397
diff changeset
20 ## @deftypefn {Function File} {} 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
21 ## 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
22 ## 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
23 ##
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 ## 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
25 ## 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
26 ## 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
27 ## be equal to @code{size (@var{vals}, @var{dim})}.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 ##
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
29 ## 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
30 ## 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
31 ## defined it determines this extent.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 ## 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
34 ## 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
35 ## @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
36 ## 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
37 ## 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
38 ## @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
39 ## 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
40 ##
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
41 ## 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
42 ## 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
43 ## value allows these values to be defined.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44 ##
10397
180931276a52 fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents: 10396
diff changeset
45 ## An example of the use of @code{accumdim} is:
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 ##
14116
951eacaf9381 Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14113
diff changeset
47 ## @example
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
48 ## @group
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
49 ## 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
50 ## -5, -12, 8;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
51 ## -12, 2, 8;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
52 ## -10, 9, -3;
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
53 ## -5, -3, -13])
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
54 ## @result{} [-10,-11,-1;-15,-3,5]
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 ## @end group
14116
951eacaf9381 Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14113
diff changeset
56 ## @end example
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
57 ##
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58 ## @seealso{accumarray}
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 ## @end deftypefn
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
61 function A = accumdim (subs, vals, dim, n = 0, func = [], fillval = 0)
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62
16776
65546674d336 accumdim.m: fix bug and allow 6 arguments (bug #38770)
Corey Thomasson <cthom.lists@gmail.com>
parents: 15202
diff changeset
63 if (nargin < 2 || nargin > 6)
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 print_usage ();
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 if (isempty (fillval))
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 fillval = 0;
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 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
72 error ("accumdim: SUBS must be a subscript vector");
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 elseif (! isindex (subs)) # creates index cache
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 error ("accumdim: indices must be positive integers");
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 else
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 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
77 if (n == 0 || isempty (n))
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 n = m;
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
79 elseif (n < m)
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
80 error ("accumdim: N index out of range");
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 11191
diff changeset
84 sz = size (vals);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86 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
87 ## 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
88 (dim = find (sz > 1, 1)) || (dim = 1);
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
89 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
90 error ("accumdim: DIM must be a valid dimension");
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
91 elseif (dim > length (sz))
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
92 sz(end+1:dim) = 1;
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94 sz(dim) = n;
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
95
14113
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
96 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
97 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
98 endif
dac62c415e8b Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 12974
diff changeset
99
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
100 if (isempty (func) || func == @sum)
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
101 ## 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
102 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
103
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
104 ## Fill in nonzero fill value
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
105 if (fillval != 0)
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
106 mask = true (n, 1);
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
107 mask(subs) = false;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
108 subsc = {':'}(ones (1, length (sz)));
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
109 subsc{dim} = mask;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
110 A(subsc{:}) = fillval;
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
111 endif
17312
088d014a7fe2 Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents: 16777
diff changeset
112 return;
10396
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
113 endif
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
114
a0b51ac0f88a optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents: 10395
diff changeset
115 ## The general case.
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116 ns = length (subs);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
117 ## Sort indices.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 [subs, idx] = sort (subs(:));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
119 ## Identify runs.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120 jdx = find (subs(1:ns-1) != subs(2:ns));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 jdx = [jdx; ns];
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 ## Collect common slices.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 szc = num2cell (sz);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 szc{dim} = diff ([0; jdx]);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 subsc = {':'}(ones (1, length (sz)));
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 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
127 vals = mat2cell (vals(subsc{:}), szc{:});
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 ## Apply reductions. Special case min, max.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 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
130 vals = cellfun (func, vals, {[]}, {dim}, "uniformoutput", false);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131 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
132 vals = cellfun (func, vals, {dim}, "uniformoutput", false);
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 endif
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 subs = subs(jdx);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 ## 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
137 vals = cat (dim, vals{:});
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
139 ## Construct matrix of fillvals.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
140 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
141 A = zeros (sz, class (vals));
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142 else
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 A = repmat (fillval, sz);
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144 endif
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 ## Set the reduced values.
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147 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
148 A(subsc{:}) = vals;
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 endfunction
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
152
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
153 ## Test accumdim vs. accumarray
10395
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 %!shared a
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 %! a = rand (5, 5, 5);
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 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3)))
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2), accumarray ([2;3;2;2;2], a(4,:,2), [1,4]))
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 %!assert (accumdim ([2;3;2;1;2], a, 3, 3, @min)(1,5,:), accumarray ([2;3;2;1;2], a(1,5,:), [1,1,3], @min))
aeb5b1e47978 new function: accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 %!assert (accumdim ([1;3;2;2;1], a, 2, 3, @median)(4,:,5), 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
161
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
162 ## Test fillval
16777
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
163 %!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
164 %!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
165 %!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
166
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
167 ## Test input validation
16777
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
168 %!error accumdim (1)
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
169 %!error accumdim (1,2,3,4,5,6,7)
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
170 %!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
171 %!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
172 %!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
173 %!error <dimension mismatch> accumdim ([1], ones (2,2))
59b08464d971 accumdim.m: Add %!tests for function.
Rik <rik@octave.org>
parents: 16776
diff changeset
174