Mercurial > octave-nkf
comparison scripts/general/accumdim.m @ 10395:aeb5b1e47978
new function: accumdim
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 05 Mar 2010 11:11:01 +0100 |
parents | |
children | a0b51ac0f88a |
comparison
equal
deleted
inserted
replaced
10394:bc475cd49147 | 10395:aeb5b1e47978 |
---|---|
1 ## Copyright (C) 2010 VZLU Prague | |
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 | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
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 | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} accumdim (@var{subs}, @var{vals}, @var{dim}, @var{sz}, @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. | |
26 ## | |
27 ## The extent of the result matrix in the working dimension will be determined | |
28 ## by the subscripts themselves. | |
29 ## However, if @var{n} is defined it determines this extent. | |
30 ## | |
31 ## The default action of @code{accumdim} is to sum the subarrays with the | |
32 ## same subscripts. This behavior can be modified by defining the @var{func} | |
33 ## function. This should be a function or function handle that accepts an | |
34 ## array and a dimension, and reduces the array along this dimension. | |
35 ## As a special exception, the built-in @code{min} and @code{max} functions | |
36 ## can be used directly, and @code{accumdim} accounts for the middle empty | |
37 ## argument that is used in their calling. | |
38 ## | |
39 ## The slices of the returned array that have no subscripts associated with | |
40 ## them are set to zero. Defining @var{fillval} to some other value allows | |
41 ## these values to be defined. | |
42 ## | |
43 ## An example of the use of @code{accumarray} is: | |
44 ## | |
45 ## @example | |
46 ## @group | |
47 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2], 101:105) | |
48 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] | |
49 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] | |
50 ## @end group | |
51 ## @end example | |
52 ## | |
53 ## @seealso{accumarray} | |
54 ## @end deftypefn | |
55 | |
56 function A = accumdim (subs, val, dim, n = 0, func = [], fillval = 0) | |
57 | |
58 if (nargin < 2 || nargin > 5) | |
59 print_usage (); | |
60 endif | |
61 | |
62 if (isempty (fillval)) | |
63 fillval = 0; | |
64 endif | |
65 | |
66 if (isempty (func)) | |
67 func = @sum; | |
68 endif | |
69 | |
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"); | |
74 else | |
75 m = max (subs); | |
76 if (n == 0) | |
77 n = m; | |
78 elseif (n < m) | |
79 error ("accumdim: index out of range") | |
80 endif | |
81 endif | |
82 | |
83 ## The general case. | |
84 sz = size (val); | |
85 | |
86 if (nargin < 3) | |
87 [~, dim] = max (sz != 1); # first non-singleton dim | |
88 elseif (! isindex (dim, ndims (val))) | |
89 error ("accumdim: dim must be a valid dimension"); | |
90 endif | |
91 sz(dim) = n; | |
92 | |
93 ns = length (subs); | |
94 ## Sort indices. | |
95 [subs, idx] = sort (subs(:)); | |
96 ## Identify runs. | |
97 jdx = find (subs(1:ns-1) != subs(2:ns)); | |
98 jdx = [jdx; ns]; | |
99 ## Collect common slices. | |
100 szc = num2cell (sz); | |
101 szc{dim} = diff ([0; jdx]); | |
102 subsc = {':'}(ones (1, length (sz))); | |
103 subsc{dim} = idx; | |
104 val = mat2cell (val(subsc{:}), szc{:}); | |
105 ## Apply reductions. Special case min, max. | |
106 if (func == @min || func == @max) | |
107 val = cellfun (func, val, {[]}, {dim}, "uniformoutput", false); | |
108 else | |
109 val = cellfun (func, val, {dim}, "uniformoutput", false); | |
110 endif | |
111 subs = subs(jdx); | |
112 | |
113 ## Concatenate reduced slices. | |
114 val = cat (dim, val{:}); | |
115 | |
116 ## Construct matrix of fillvals. | |
117 if (fillval == 0) | |
118 A = zeros (sz, class (val)); | |
119 else | |
120 A = repmat (fillval, sz); | |
121 endif | |
122 | |
123 ## Set the reduced values. | |
124 subsc{dim} = subs; | |
125 A(subsc{:}) = val; | |
126 | |
127 endfunction | |
128 | |
129 %%test accumdim vs. accumarray | |
130 | |
131 %!shared a | |
132 %! a = rand (5, 5, 5); | |
133 | |
134 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3))) | |
135 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2), accumarray ([2;3;2;2;2], a(4,:,2), [1,4])) | |
136 %!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)) | |
137 %!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)) |