1 ## Copyright (C) 2010-2012 VZLU Prague
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} accumdim (@var{subs}, @var{vals}, @var{dim}, @var{n}, @var{func}, @var{fillval})
21 ## Create an array by accumulating the slices of an array into the
22 ## positions defined by their subscripts along a specified dimension.
23 ## The subscripts are defined by the index vector @var{subs}.
24 ## The dimension is specified by @var{dim}. If not given, it defaults
25 ## to the first non-singleton dimension. The length of @var{subs} must
26 ## be equal to @code{size (@var{vals}, @var{dim})}.
28 ## The extent of the result matrix in the working dimension will be
29 ## determined by the subscripts themselves. However, if @var{n} is
30 ## defined it determines this extent.
32 ## The default action of @code{accumdim} is to sum the subarrays with the
33 ## same subscripts. This behavior can be modified by defining the
34 ## @var{func} function. This should be a function or function handle
35 ## that accepts an array and a dimension, and reduces the array along
36 ## this dimension. As a special exception, the built-in @code{min} and
37 ## @code{max} functions can be used directly, and @code{accumdim}
38 ## accounts for the middle empty argument that is used in their calling.
40 ## The slices of the returned array that have no subscripts associated
41 ## with them are set to zero. Defining @var{fillval} to some other
42 ## value allows these values to be defined.
44 ## An example of the use of @code{accumdim} is:
48 ## accumdim ([1, 2, 1, 2, 1], [ 7, -10, 4;
53 ## @result{} [-10,-11,-1;-15,-3,5]
57 ## @seealso{accumarray}
60 function A = accumdim (subs, vals, dim, n = 0, func = [], fillval = 0)
62 if (nargin < 2 || nargin > 5)
66 if (isempty (fillval))
70 if (! isvector (subs))
71 error ("accumdim: SUBS must be a subscript vector");
72 elseif (! isindex (subs)) # creates index cache
73 error ("accumdim: indices must be positive integers");
76 if (n == 0 || isempty (n))
79 error ("accumdim: N index out of range");
86 [~, dim] = max (sz != 1); # first non-singleton dim
87 elseif (! isindex (dim))
88 error ("accumdim: DIM must be a valid dimension");
89 elseif (dim > length (sz))
94 if (length (subs) != size (vals, dim))
95 error ("accumdim: dimension mismatch")
98 if (isempty (func) || func == @sum)
99 ## Fast summation case.
100 A = __accumdim_sum__ (subs, vals, dim, n);
102 ## Fill in nonzero fill value
106 subsc = {':'}(ones (1, length (sz)));
108 A(subsc{:}) = fillval;
116 [subs, idx] = sort (subs(:));
118 jdx = find (subs(1:ns-1) != subs(2:ns));
120 ## Collect common slices.
122 szc{dim} = diff ([0; jdx]);
123 subsc = {':'}(ones (1, length (sz)));
125 vals = mat2cell (vals(subsc{:}), szc{:});
126 ## Apply reductions. Special case min, max.
127 if (func == @min || func == @max)
128 vals = cellfun (func, vals, {[]}, {dim}, "uniformoutput", false);
130 vals = cellfun (func, vals, {dim}, "uniformoutput", false);
134 ## Concatenate reduced slices.
135 vals = cat (dim, vals{:});
137 ## Construct matrix of fillvals.
139 A = zeros (sz, class (vals));
141 A = repmat (fillval, sz);
144 ## Set the reduced values.
150 %%test accumdim vs. accumarray
153 %! a = rand (5, 5, 5);
155 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3)))
156 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2), accumarray ([2;3;2;2;2], a(4,:,2), [1,4]))
157 %!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))
158 %!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))