6770
|
1 ## Copyright (C) 2007 David Bateman |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{fun}, @var{fillval}, @var{issparse}) |
|
20 ## @deftypefnx {Function File} {} accumarray (@var{csubs}, @var{vals}, @dots{}) |
|
21 ## |
|
22 ## Creates an array by accumulating the elements of a vector into the |
|
23 ## positions of defined by their subscripts. The subscripts are defined by |
|
24 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each row |
|
25 ## of @var{subs} corresponds to one of the values in @var{vals}. |
|
26 ## |
|
27 ## The size of the matrix will be determined by the subscripts themselves. |
|
28 ## However, if @var{sz} is defined it determines the matrix size. The length |
|
29 ## of @var{sz} must correspond to the number of columns in @var{subs}. |
|
30 ## |
|
31 ## The default action of @code{accumarray} is to sum the elements with the |
|
32 ## same subscripts. This behavior can be modified by defining the @var{fun} |
|
33 ## function. This should be a function or function handle that accepts a |
|
34 ## column vector and returns a scalar. The result of the function should not |
|
35 ## depend on the order of the subscripts. |
|
36 ## |
|
37 ## The elements of the returned array that have no subscripts assoicated with |
|
38 ## them are set to zero. Defining @var{fillval} to some other value allows |
|
39 ## these values to be defined. |
|
40 ## |
|
41 ## By default @code{accumarray} returns a full matrix. If @var{issparse} is |
|
42 ## logically true, then a sparse matrix is returned instead. |
|
43 ## |
|
44 ## An example of the use of @code{accumarray} is: |
|
45 ## |
|
46 ## @example |
|
47 ## @group |
|
48 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105) |
|
49 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] |
|
50 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] |
|
51 ## @end group |
|
52 ## @end example |
|
53 ## @end deftypefn |
|
54 |
|
55 function A = accumarray (subs, val, sz, fun, fillval, isspar) |
|
56 |
|
57 if (nargin < 2 || nargin > 6) |
|
58 print_usage (); |
|
59 endif |
|
60 |
|
61 if (iscell(subs)) |
|
62 subs = cell2mat (cellfun (@(x) x(:), subs, 'UniformOutput', false)); |
|
63 endif |
|
64 ndims = size (subs, 2); |
|
65 |
|
66 if (nargin < 3 || isempty (sz)) |
|
67 sz = max (subs); |
|
68 if (isscalar(sz)) |
|
69 sz = [sz, 1]; |
|
70 endif |
|
71 else |
|
72 if (length (sz) != ndims) |
|
73 error ("accumarray: inconsistent dimensions"); |
|
74 endif |
|
75 endif |
|
76 |
|
77 if (nargin < 4 || isempty (fun)) |
|
78 fun = @sum; |
|
79 endif |
|
80 |
|
81 if (nargin < 5 || isempty (fillval)) |
|
82 fillval = 0; |
|
83 endif |
|
84 |
|
85 if (nargin < 6 || isempty (isspar)) |
|
86 isspar = false; |
|
87 endif |
|
88 if (isspar) |
|
89 if (ndims > 2) |
|
90 error ("Can not have more than 2 dimensions in a sparse matrix"); |
|
91 endif |
|
92 endif |
|
93 |
|
94 [subs, idx] = sortrows (subs); |
|
95 if (isscalar (val)) |
|
96 val = val * ones (size (idx)); |
|
97 else |
|
98 val = val(idx); |
|
99 endif |
|
100 cidx = find([true; (sum (abs (diff (subs)), 2) != 0)]); |
|
101 idx = cell (1, ndims); |
|
102 for i = 1:ndims |
|
103 idx{i} = subs (cidx, i); |
|
104 endfor |
|
105 x = cellfun (fun, mat2cell (val(:), diff ([cidx; length(val) + 1]))); |
|
106 if (isspar && fillval == 0) |
|
107 A = sparse (idx{1}, idx{2}, x, sz(1), sz(2)); |
|
108 else |
|
109 if (iscell (x)) |
|
110 ## Why did matlab choose to reverse the order of the elements |
|
111 x = cellfun (@(x) flipud(x(:)), x, 'UniformOutput', false); |
|
112 A = cell (sz); |
|
113 elseif (fillval == 0) |
|
114 A = zeros (sz, class(x)); |
|
115 else |
|
116 A = fillval .* ones (sz); |
|
117 endif |
|
118 A (sub2ind (sz, idx{:})) = x; |
|
119 endif |
|
120 endfunction |
|
121 |
|
122 %!error (accumarray (1:5)) |
|
123 %!error (accumarray ([1,2,3],1:2)) |
|
124 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208]) |
|
125 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105),cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208])) |
|
126 %!assert (accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2],101:105,[],@(x)sin(sum(x))),sin(cat(3, [101,0,0;0,0,0],[0,0,0;206,0,208]))) |
|
127 %!assert (accumarray ({[1 3 3 2 3 1 2 2 3 3 1 2],[3 4 2 1 4 3 4 2 2 4 3 4],[1 1 2 2 1 1 2 1 1 1 2 2]},101:112),cat(3,[0,0,207,0;0,108,0,0;0,109,0,317],[0,0,111,0;104,0,0,219;0,103,0,0])) |
|
128 %!assert (accumarray ([1,1;2,1;2,3;2,1;2,3],101:105,[2,4],@max,NaN),[101,NaN,NaN,NaN;104,NaN,105,NaN]) |
|
129 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2 4],@prod,0,true),sparse([1,2,2],[1,1,3],[101,10608,10815],2,4)) |
|
130 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0]) |
|
131 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x)length(x)>1),[false,false,false,false;true,false,true,false]) |
|
132 %!test |
|
133 %! A = accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x){x}); |
|
134 %! assert (A{2},[104;102]) |