Mercurial > hg > octave-lyh
annotate scripts/general/accumarray.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 89b95972e178 |
children | c2099a4d12ea |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 David Bateman |
6770 | 2 ## |
7016 | 3 ## This file is part of Octave. |
6770 | 4 ## |
7016 | 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. | |
6770 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6770 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{fun}, @var{fillval}, @var{issparse}) | |
21 ## @deftypefnx {Function File} {} accumarray (@var{csubs}, @var{vals}, @dots{}) | |
22 ## | |
7186 | 23 ## Create an array by accumulating the elements of a vector into the |
24 ## positions defined by their subscripts. The subscripts are defined by | |
6770 | 25 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each row |
26 ## of @var{subs} corresponds to one of the values in @var{vals}. | |
27 ## | |
28 ## The size of the matrix will be determined by the subscripts themselves. | |
29 ## However, if @var{sz} is defined it determines the matrix size. The length | |
30 ## of @var{sz} must correspond to the number of columns in @var{subs}. | |
31 ## | |
32 ## The default action of @code{accumarray} is to sum the elements with the | |
33 ## same subscripts. This behavior can be modified by defining the @var{fun} | |
34 ## function. This should be a function or function handle that accepts a | |
35 ## column vector and returns a scalar. The result of the function should not | |
36 ## depend on the order of the subscripts. | |
37 ## | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7186
diff
changeset
|
38 ## The elements of the returned array that have no subscripts associated with |
6770 | 39 ## them are set to zero. Defining @var{fillval} to some other value allows |
40 ## these values to be defined. | |
41 ## | |
42 ## By default @code{accumarray} returns a full matrix. If @var{issparse} is | |
43 ## logically true, then a sparse matrix is returned instead. | |
44 ## | |
45 ## An example of the use of @code{accumarray} is: | |
46 ## | |
47 ## @example | |
48 ## @group | |
7186 | 49 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2], 101:105) |
6770 | 50 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] |
51 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] | |
52 ## @end group | |
53 ## @end example | |
54 ## @end deftypefn | |
55 | |
56 function A = accumarray (subs, val, sz, fun, fillval, isspar) | |
57 | |
58 if (nargin < 2 || nargin > 6) | |
59 print_usage (); | |
60 endif | |
61 | |
7186 | 62 if (iscell (subs)) |
8507 | 63 subs = cell2mat (cellfun (@(x) x(:), subs, "UniformOutput", false)); |
6770 | 64 endif |
65 ndims = size (subs, 2); | |
66 | |
67 if (nargin < 3 || isempty (sz)) | |
68 sz = max (subs); | |
69 if (isscalar(sz)) | |
70 sz = [sz, 1]; | |
71 endif | |
7186 | 72 elseif (length (sz) != ndims |
73 && (ndims != 1 || length (sz) != 2 || sz(2) != 1)) | |
74 error ("accumarray: inconsistent dimensions"); | |
6770 | 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 | |
7186 | 88 |
89 if (isspar && ndims > 2) | |
90 error ("accumarray: sparse matrices limited to 2 dimensions"); | |
6770 | 91 endif |
92 | |
93 [subs, idx] = sortrows (subs); | |
8820
89b95972e178
fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents:
8507
diff
changeset
|
94 |
6770 | 95 if (isscalar (val)) |
96 val = val * ones (size (idx)); | |
97 else | |
98 val = val(idx); | |
99 endif | |
7186 | 100 cidx = find ([true; (sum (abs (diff (subs)), 2) != 0)]); |
6770 | 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 | |
8507 | 111 x = cellfun (@(x) flipud (x(:)), x, "UniformOutput", false); |
6770 | 112 A = cell (sz); |
113 elseif (fillval == 0) | |
7186 | 114 A = zeros (sz, class (x)); |
6770 | 115 else |
116 A = fillval .* ones (sz); | |
117 endif | |
7186 | 118 A(sub2ind (sz, idx{:})) = x; |
6770 | 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]) |