annotate scripts/general/accumarray.m @ 10275:19f2107d1fdd

document accumarray complexity
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 08 Feb 2010 13:22:09 +0100
parents db613bccd992
children 703038d648f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8820
diff changeset
1 ## Copyright (C) 2007, 2008, 2009 David Bateman
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
3 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
4 ## This file is part of Octave.
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
5 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
9 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
10 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
14 ## General Public License for more details.
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
15 ##
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6770
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
19
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
9163
9cb0c21e97f7 Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
21 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{func}, @var{fillval}, @var{issparse})
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
22 ## @deftypefnx {Function File} {} accumarray (@var{csubs}, @var{vals}, @dots{})
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
23 ##
7186
800f1fd3ffb8 [project @ 2007-11-26 19:16:07 by jwe]
jwe
parents: 7016
diff changeset
24 ## Create an array by accumulating the elements of a vector into the
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
25 ## positions defined by their subscripts. The subscripts are defined by
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
26 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each row
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
27 ## of @var{subs} corresponds to one of the values in @var{vals}.
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
28 ##
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
29 ## The size of the matrix will be determined by the subscripts themselves.
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
30 ## However, if @var{sz} is defined it determines the matrix size. The length
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
31 ## of @var{sz} must correspond to the number of columns in @var{subs}.
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
32 ##
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
33 ## The default action of @code{accumarray} is to sum the elements with the
9163
9cb0c21e97f7 Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
34 ## same subscripts. This behavior can be modified by defining the @var{func}
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
35 ## function. This should be a function or function handle that accepts a
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
36 ## column vector and returns a scalar. The result of the function should not
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
37 ## depend on the order of the subscripts.
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
38 ##
8325
b93ac0586e4b spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents: 7186
diff changeset
39 ## The elements of the returned array that have no subscripts associated with
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
40 ## them are set to zero. Defining @var{fillval} to some other value allows
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
41 ## these values to be defined.
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
42 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8934
diff changeset
43 ## By default @code{accumarray} returns a full matrix. If @var{issparse} is
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
44 ## logically true, then a sparse matrix is returned instead.
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
45 ##
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
46 ## An example of the use of @code{accumarray} is:
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
47 ##
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
48 ## @example
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
49 ## @group
7186
800f1fd3ffb8 [project @ 2007-11-26 19:16:07 by jwe]
jwe
parents: 7016
diff changeset
50 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2], 101:105)
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
51 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0]
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
52 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208]
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
53 ## @end group
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
54 ## @end example
10275
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
55 ##
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
56 ## The complexity in the non-sparse case is generally O(M+N), where N is the number of
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
57 ## subscripts and M is the maximum subscript (linearized in multidimensional case).
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
58 ## If @var{func} is one of @code{@@sum} (default), @code{@@max}, @code{@@min}
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
59 ## or @code{@@(x) @{x@}}, an optimized code path is used.
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
60 ## Note that for general reduction function the interpreter overhead can play a
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
61 ## major part and it may be more efficient to do multiple accumarray calls and
19f2107d1fdd document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents: 10274
diff changeset
62 ## compute the results in a vectorized manner.
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
63 ## @end deftypefn
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
64
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
65 function A = accumarray (subs, val, sz = [], func = [], fillval = [], isspar = [])
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
66
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
67 if (nargin < 2 || nargin > 6)
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
68 print_usage ();
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
69 endif
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
70
7186
800f1fd3ffb8 [project @ 2007-11-26 19:16:07 by jwe]
jwe
parents: 7016
diff changeset
71 if (iscell (subs))
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
72 subs = cellfun (@(x) x(:), subs, "UniformOutput", false);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
73 ndims = numel (subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
74 if (ndims == 1)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
75 subs = subs{1};
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
76 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
77 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
78 ndims = columns (subs);
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
79 endif
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
80
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
81 if (isempty (fillval))
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
82 fillval = 0;
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
83 endif
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
84
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
85 if (isempty (isspar))
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
86 isspar = false;
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
87 endif
7186
800f1fd3ffb8 [project @ 2007-11-26 19:16:07 by jwe]
jwe
parents: 7016
diff changeset
88
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
89 if (isspar)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
90
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
91 ## Sparse case. Avoid linearizing the subscripts, because it could overflow.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
92
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
93 if (fillval != 0)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
94 error ("accumarray: fillval must be zero in the sparse case");
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
95 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
96
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
97 ## Ensure subscripts are a two-column matrix.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
98 if (iscell (subs))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
99 subs = [subs{:}];
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
100 endif
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
101
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
102 ## Validate dimensions.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
103 if (ndims == 1)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
104 subs(:,2) = 1;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
105 elseif (ndims != 2)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
106 error ("accumarray: in the sparse case, needs 1 or 2 subscripts");
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
107 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
108
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
109 if (isnumeric (val) || islogical (val))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
110 vals = double (val);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
111 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
112 error ("accumarray: in the sparse case, values must be numeric or logical");
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
113 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
114
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
115 if (! (isempty (func) || func == @sum))
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
116
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
117 ## Reduce values. This is not needed if we're about to sum them, because
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
118 ## "sparse" can do that.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
119
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
120 ## Sort indices.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
121 [subs, idx] = sortrows (subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
122 n = rows (subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
123 ## Identify runs.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
124 jdx = find (any (diff (subs, 1, 1), 2));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
125 jdx = [jdx; n];
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
126
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
127 val = cellfun (func, mat2cell (val(:)(idx), diff ([0; jdx])));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
128 subs = subs(jdx, :);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
129 endif
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
130
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
131 ## Form the sparse matrix.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
132 if (isempty (sz))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
133 A = sparse (subs(:,1), subs(:,2), val);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
134 elseif (length (sz) == 2)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
135 A = sparse (subs(:,1), subs(:,2), val, sz(1), sz(2));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
136 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
137 error ("accumarray: dimensions mismatch")
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
138 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
139
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
140 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
141
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
142 ## Linearize subscripts.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
143 if (ndims > 1)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
144 if (isempty (sz))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
145 if (iscell (subs))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
146 sz = cellfun (@max, subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
147 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
148 sz = max (subs, [], 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
149 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
150 elseif (ndims != length (sz))
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
151 error ("accumarray: dimensions mismatch")
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
152 endif
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
153
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
154 ## Convert multidimensional subscripts.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
155 if (ismatrix (subs))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
156 subs = num2cell (subs, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
157 endif
10274
db613bccd992 take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10271
diff changeset
158 subs = sub2ind (sz, subs{:}); # creates index cache
10271
297996005012 1 more small fix in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10269
diff changeset
159 elseif (! isempty (sz) && length (sz) < 2)
10269
217d36560dfa small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10268
diff changeset
160 error ("accumarray: needs at least 2 dimensions");
10274
db613bccd992 take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10271
diff changeset
161 elseif (! isindex (subs)) # creates index cache
db613bccd992 take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10271
diff changeset
162 error ("accumarray: indices must be positive integers");
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
163 endif
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
164
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
165
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
166 ## Some built-in reductions handled efficiently.
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
167
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
168 if (isempty (func) || func == @sum)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
169 ## Fast summation.
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
170 if (isempty (sz))
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
171 A = __accumarray_sum__ (subs, val);
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
172 else
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
173 A = __accumarray_sum__ (subs, val, prod (sz));
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
174 ## set proper shape.
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
175 A = reshape (A, sz);
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
176 endif
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
177
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
178 ## we fill in nonzero fill value.
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
179 if (fillval != 0)
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
180 mask = true (size (A));
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
181 mask(subs) = false;
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
182 A(mask) = fillval;
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
183 endif
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
184 elseif (func == @max)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
185 ## Fast maximization.
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
186
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
187 if (isinteger (val))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
188 zero = intmin (class (val));
10269
217d36560dfa small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10268
diff changeset
189 elseif (islogical (val))
217d36560dfa small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10268
diff changeset
190 zero = false;
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
191 elseif (fillval == 0 && all (val(:) >= 0))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
192 ## This is a common case - fillval is zero, all numbers nonegative.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
193 zero = 0;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
194 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
195 zero = NaN; # Neutral value.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
196 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
197
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
198 if (isempty (sz))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
199 A = __accumarray_max__ (subs, val, zero);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
200 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
201 A = __accumarray_max__ (subs, val, zero, prod (sz));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
202 A = reshape (A, sz);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
203 endif
8934
c2099a4d12ea partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
204
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
205 if (fillval != zero && isnan (fillval) != isnan (zero))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
206 mask = true (size (A));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
207 mask(subs) = false;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
208 A(mask) = fillval;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
209 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
210 elseif (func == @min)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
211 ## Fast minimization.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
212
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
213 if (isinteger (val))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
214 zero = intmax (class (val));
10269
217d36560dfa small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10268
diff changeset
215 elseif (islogical (val))
217d36560dfa small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 10268
diff changeset
216 zero = true;
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
217 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
218 zero = NaN; # Neutral value.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
219 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
220
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
221 if (isempty (sz))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
222 A = __accumarray_min__ (subs, val, zero);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
223 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
224 A = __accumarray_min__ (subs, val, zero, prod (sz));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
225 A = reshape (A, sz);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
226 endif
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
227
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
228 if (fillval != zero && isnan (fillval) != isnan (zero))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
229 mask = true (size (A));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
230 mask(subs) = false;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
231 A(mask) = fillval;
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
232 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
233 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
234
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
235 ## The general case. Reduce values.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
236 n = rows (subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
237 if (numel (val) == 1)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
238 val = val(ones (1, n), 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
239 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
240 val = val(:);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
241 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
242
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
243 ## Sort indices.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
244 [subs, idx] = sort (subs);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
245 ## Identify runs.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
246 jdx = find (diff (subs, 1, 1));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
247 jdx = [jdx; n];
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
248 val = mat2cell (val(idx), diff ([0; jdx]));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
249 ## Optimize the case when function is @(x) {x}, i.e. we just want to
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
250 ## collect the values to cells.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
251 persistent simple_cell_str = func2str (@(x) {x});
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
252 if (! strcmp (func2str (func), simple_cell_str))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
253 val = cellfun (func, val);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
254 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
255 subs = subs(jdx);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
256
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
257 ## Construct matrix of fillvals.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
258 if (iscell (val))
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
259 A = cell (sz);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
260 elseif (fillval == 0)
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
261 A = zeros (sz, class (val));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
262 else
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
263 A = repmat (fillval, sz);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
264 endif
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
265
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
266 ## Set the reduced values.
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
267 A(subs) = val;
6770
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
268 endif
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
269 endif
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
270 endfunction
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
271
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
272 %!error (accumarray (1:5))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
273 %!error (accumarray ([1,2,3],1:2))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
274 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208])
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
275 %!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]))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
276 %!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])))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
277 %!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]))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
278 %!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])
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
279 %!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))
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
280 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0])
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
281 %!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])
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
282 %!test
babcbcc11ab6 [project @ 2007-07-06 08:44:04 by dbateman]
dbateman
parents:
diff changeset
283 %! A = accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],101:105,[2,4],@(x){x});
10268
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
284 %! assert (A{2},[102;104])
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
285 %!test
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
286 %! subs = ceil (rand (2000, 3)*10);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
287 %! val = rand (2000, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
288 %! assert (accumarray (subs, val, [], @max), accumarray (subs, val, [], @(x) max (x)));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
289 %!test
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
290 %! subs = ceil (rand (2000, 1)*100);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
291 %! val = rand (2000, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
292 %! assert (accumarray (subs, val, [100, 1], @min, NaN), accumarray (subs, val, [100, 1], @(x) min (x), NaN));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
293 %!test
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
294 %! subs = ceil (rand (2000, 2)*30);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
295 %! subsc = num2cell (subs, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
296 %! val = rand (2000, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
297 %! assert (accumarray (subsc, val, [], [], 0, true), accumarray (subs, val, [], [], 0, true));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
298 %!test
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
299 %! subs = ceil (rand (2000, 3)*10);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
300 %! subsc = num2cell (subs, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
301 %! val = rand (2000, 1);
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
302 %! assert (accumarray (subsc, val, [], @max), accumarray (subs, val, [], @max));
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
303
9a16a61ed43d new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents: 9859
diff changeset
304