Mercurial > hg > octave-nkf
annotate scripts/general/accumarray.m @ 13929:9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
* accumarray.m, blkdiag.m, nargoutchk.m, nthargout.m, profexplore.m, profile.m,
computer.m, orderfields.m, recycle.m, version.m, sqp.m, matlabroot.m,
__plt_get_axis_arg__.m, isonormals.m, isosurface.m, __fltk_file_filter__.m,
__is_function__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiobject_split_args__.m, __uiputfile_fltk__.m, uicontextmenu.m, uiresume.m,
uiwait.m, mkpp.m, ppder.m, residue.m, addpref.m, getpref.m, ispref.m,
loadprefs.m, prefsfile.m, saveprefs.m, rmpref.m, setpref.m, fftshift.m, bicg.m,
bicgstab.m, cgs.m, gmres.m, __sprand_impl__.m, quantile.m, deblank.m,
strsplit.m, addtodate.m, bsxfun.cc, kron.cc, regexp.cc, data.cc, file-io.cc,
graphics.cc, load-save.cc, mappers.cc: Grammarcheck of documentation
before 3.6.0 release.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 23 Nov 2011 08:38:19 -0800 |
parents | 9ab64f063c96 |
children | 9de488c6c59c |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2007-2011 David Bateman |
2 ## Copyright (C) 2009-2010 VZLU Prague | |
6770 | 3 ## |
7016 | 4 ## This file is part of Octave. |
6770 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
6770 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6770 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10775
diff
changeset
|
21 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{func}, @var{fillval}, @var{issparse}) |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
22 ## @deftypefnx {Function File} {} accumarray (@var{subs}, @var{vals}, @dots{}) |
6770 | 23 ## |
7186 | 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 |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
26 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
27 ## row of @var{subs} corresponds to one of the values in @var{vals}. If |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
28 ## @var{vals} is a scalar, it will be used for each of the row of |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
29 ## @var{subs}. |
6770 | 30 ## |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
31 ## The size of the matrix will be determined by the subscripts |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
32 ## themselves. However, if @var{sz} is defined it determines the matrix |
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
33 ## size. The length of @var{sz} must correspond to the number of columns |
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
34 ## in @var{subs}. An exception is if @var{subs} has only one column, in |
13788
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
35 ## which case @var{sz} may be the dimensions of a vector and the subscripts |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
36 ## of @var{subs} are taken as the indices into it. |
6770 | 37 ## |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
38 ## The default action of @code{accumarray} is to sum the elements with |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
39 ## the same subscripts. This behavior can be modified by defining the |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
40 ## @var{func} function. This should be a function or function handle |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
41 ## that accepts a column vector and returns a scalar. The result of the |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
42 ## function should not depend on the order of the subscripts. |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
43 ## |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
44 ## The elements of the returned array that have no subscripts associated |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
45 ## with them are set to zero. Defining @var{fillval} to some other value |
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
46 ## allows these values to be defined. This behaviour changes, however, |
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
47 ## for certain values of @var{func}. If @var{func} is @code{min} |
13787
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
48 ## (respectively, @code{max}) then the result will be filled with the |
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
49 ## minimum (respectively, maximum) integer if @var{vals} is of integral |
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
50 ## type, logical false (respectively, logical true) if @var{vals} is of |
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
51 ## logical type, zero if @var{fillval} is zero and all values are |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
52 ## nonpositive (respectively, non-negative), and NaN otherwise. |
6770 | 53 ## |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
54 ## By default @code{accumarray} returns a full matrix. If |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
55 ## @var{issparse} is logically true, then a sparse matrix is returned |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
56 ## instead. |
6770 | 57 ## |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
58 ## The following @code{accumarray} example constructs a frequency table |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
59 ## that in the first column counts how many occurrences each number in |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
60 ## the second column has, taken from the vector @var{x}. Note the usage |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
61 ## of @code{unique} for assigning to all repeated elements of @var{x} |
13172
25467719730b
doc: Quiet Texinfo warning in accumarray.m
Rik <octave@nomad.inbox5.com>
parents:
13152
diff
changeset
|
62 ## the same index (@pxref{doc-unique}). |
6770 | 63 ## |
64 ## @example | |
65 ## @group | |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
66 ## x = [91, 92, 90, 92, 90, 89, 91, 89, 90, 100, 100, 100]; |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
67 ## [u, ~, j] = unique (x); |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
68 ## [accumarray(j', 1), u'] |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
69 ## @result{} 2 89 |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
70 ## 3 90 |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
71 ## 2 91 |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
72 ## 2 92 |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
73 ## 3 100 |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
74 ## @end group |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
75 ## @end example |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
76 ## |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
77 ## Another example, where the result is a multi-dimensional 3-D array and |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
78 ## the default value (zero) appears in the output: |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
79 ## |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
80 ## @example |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
81 ## @group |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
82 ## accumarray ([1, 1, 1; |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
83 ## 2, 1, 2; |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
84 ## 2, 3, 2; |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
85 ## 2, 1, 2; |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
86 ## 2, 3, 2], 101:105) |
6770 | 87 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] |
88 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] | |
89 ## @end group | |
90 ## @end example | |
10275
19f2107d1fdd
document accumarray complexity
Jaroslav Hajek <highegg@gmail.com>
parents:
10274
diff
changeset
|
91 ## |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
92 ## The complexity in the non-sparse case is generally O(M+N), where N is |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
93 ## the number of subscripts and M is the maximum subscript (linearized |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
94 ## in multi-dimensional case). If @var{func} is one of @code{@@sum} |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
95 ## (default), @code{@@max}, @code{@@min} or @code{@@(x) @{x@}}, an |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13788
diff
changeset
|
96 ## optimized code path is used. Note that for general reduction function |
13152
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
97 ## the interpreter overhead can play a major part and it may be more |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
98 ## efficient to do multiple accumarray calls and compute the results in |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
99 ## a vectorized manner. |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
100 ## |
8d5f0b41e6b0
Improve accumarray's docstring: one more example, mention vals can be scalar
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12214
diff
changeset
|
101 ## @seealso{accumdim, unique} |
6770 | 102 ## @end deftypefn |
103 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
104 function A = accumarray (subs, vals, sz = [], func = [], fillval = [], issparse = []) |
6770 | 105 |
106 if (nargin < 2 || nargin > 6) | |
107 print_usage (); | |
108 endif | |
109 | |
7186 | 110 if (iscell (subs)) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12214
diff
changeset
|
111 subs = cellfun ("vec", subs, "uniformoutput", false); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
112 ndims = numel (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
113 if (ndims == 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
114 subs = subs{1}; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
115 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
116 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
117 ndims = columns (subs); |
6770 | 118 endif |
119 | |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
120 if (isempty (fillval)) |
6770 | 121 fillval = 0; |
122 endif | |
123 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
124 if (isempty (issparse)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
125 issparse = false; |
6770 | 126 endif |
7186 | 127 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
128 if (issparse) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
129 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
130 ## Sparse case. Avoid linearizing the subscripts, because it could overflow. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
131 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
132 if (fillval != 0) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
133 error ("accumarray: FILLVAL must be zero in the sparse case"); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
134 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
135 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
136 ## Ensure subscripts are a two-column matrix. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
137 if (iscell (subs)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
138 subs = [subs{:}]; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
139 endif |
6770 | 140 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
141 ## Validate dimensions. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
142 if (ndims == 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
143 subs(:,2) = 1; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
144 elseif (ndims != 2) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
145 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
|
146 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
147 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
148 if (isnumeric (vals) || islogical (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
149 vals = double (vals); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
150 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
151 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
|
152 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
153 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
154 if (! (isempty (func) || func == @sum)) |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
155 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
156 ## 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
|
157 ## "sparse" can do that. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
158 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
159 ## Sort indices. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
160 [subs, idx] = sortrows (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
161 n = rows (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
162 ## Identify runs. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
163 jdx = find (any (diff (subs, 1, 1), 2)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
164 jdx = [jdx; n]; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
165 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
166 vals = cellfun (func, mat2cell (vals(:)(idx), diff ([0; jdx]))); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
167 subs = subs(jdx, :); |
10283
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
168 mode = "unique"; |
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
169 else |
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
170 mode = "sum"; |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
171 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
172 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
173 ## Form the sparse matrix. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
174 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
175 A = sparse (subs(:,1), subs(:,2), vals, mode); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
176 elseif (length (sz) == 2) |
13788
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
177 |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
178 ## Row vector case |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
179 if (sz(1) == 1) |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
180 [i, j] = deal (subs(:,2), subs(:,1)); |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
181 else |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
182 [i, j] = deal (subs(:,1), subs(:,2)); |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
183 endif |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
184 A = sparse (i, j, vals, sz(1), sz(2), mode); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
185 else |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
186 error ("accumarray: dimensions mismatch"); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
187 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
188 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
189 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
190 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
191 ## Linearize subscripts. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
192 if (ndims > 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
193 if (isempty (sz)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
194 if (iscell (subs)) |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12214
diff
changeset
|
195 sz = cellfun ("max", subs); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
196 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
197 sz = max (subs, [], 1); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
198 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
199 elseif (ndims != length (sz)) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
200 error ("accumarray: dimensions mismatch"); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
201 endif |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
202 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
203 ## Convert multidimensional subscripts. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
204 if (ismatrix (subs)) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
205 subs = num2cell (subs, 1); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
206 endif |
10274
db613bccd992
take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10271
diff
changeset
|
207 subs = sub2ind (sz, subs{:}); # creates index cache |
10271
297996005012
1 more small fix in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10269
diff
changeset
|
208 elseif (! isempty (sz) && length (sz) < 2) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
209 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
|
210 elseif (! isindex (subs)) # creates index cache |
db613bccd992
take advantage of new sort optimization in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10271
diff
changeset
|
211 error ("accumarray: indices must be positive integers"); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
212 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
213 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
214 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
215 ## Some built-in reductions handled efficiently. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
216 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
217 if (isempty (func) || func == @sum) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
218 ## Fast summation. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
219 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
220 A = __accumarray_sum__ (subs, vals); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
221 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
222 A = __accumarray_sum__ (subs, vals, prod (sz)); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
223 ## set proper shape. |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
224 A = reshape (A, sz); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
225 endif |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
226 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
227 ## we fill in nonzero fill value. |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
228 if (fillval != 0) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
229 mask = true (size (A)); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
230 mask(subs) = false; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
231 A(mask) = fillval; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
232 endif |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
233 elseif (func == @max) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
234 ## Fast maximization. |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
235 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
236 if (isinteger (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
237 zero = intmin (class (vals)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
238 elseif (islogical (vals)) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
239 zero = false; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
240 elseif (fillval == 0 && all (vals(:) >= 0)) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
241 ## 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
|
242 zero = 0; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
243 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
244 zero = NaN; # Neutral value. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
245 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
246 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
247 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
248 A = __accumarray_max__ (subs, vals, zero); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
249 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
250 A = __accumarray_max__ (subs, vals, zero, prod (sz)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
251 A = reshape (A, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
252 endif |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
253 |
10775 | 254 if (fillval != zero && ! (isnan (fillval) || isnan (zero))) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
255 mask = true (size (A)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
256 mask(subs) = false; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
257 A(mask) = fillval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
258 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
259 elseif (func == @min) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
260 ## Fast minimization. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
261 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
262 if (isinteger (vals)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
263 zero = intmax (class (vals)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
264 elseif (islogical (vals)) |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
265 zero = true; |
13787
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
266 elseif (fillval == 0 && all (vals(:) <= 0)) |
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
267 ## This is a common case - fillval is zero, all numbers nonpositive. |
8bb7bdbe9c69
Clarify max/min in accumarray and uniformise its behaviour
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13175
diff
changeset
|
268 zero = 0; |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
269 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
270 zero = NaN; # Neutral value. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
271 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
272 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
273 if (isempty (sz)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
274 A = __accumarray_min__ (subs, vals, zero); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
275 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
276 A = __accumarray_min__ (subs, vals, zero, prod (sz)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
277 A = reshape (A, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
278 endif |
8820
89b95972e178
fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents:
8507
diff
changeset
|
279 |
10775 | 280 if (fillval != zero && ! (isnan (fillval) || isnan (zero))) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
281 mask = true (size (A)); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
282 mask(subs) = false; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
283 A(mask) = fillval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
284 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
285 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
286 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
287 ## The general case. Reduce values. |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
288 n = rows (subs); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
289 if (numel (vals) == 1) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
290 vals = vals(ones (1, n), 1); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
291 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
292 vals = vals(:); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
293 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
294 |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
295 ## Sort indices. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
296 [subs, idx] = sort (subs); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
297 ## Identify runs. |
10283
b178769f31ca
more small improvements in accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
298 jdx = find (subs(1:n-1) != subs(2:n)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
299 jdx = [jdx; n]; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
300 vals = mat2cell (vals(idx), diff ([0; jdx])); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
301 ## 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
|
302 ## collect the values to cells. |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
303 persistent simple_cell_str = func2str (@(x) {x}); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
304 if (! strcmp (func2str (func), simple_cell_str)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
305 vals = cellfun (func, vals); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
306 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
307 subs = subs(jdx); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
308 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
309 ## Construct matrix of fillvals. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
310 if (iscell (vals)) |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
311 A = cell (sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
312 elseif (fillval == 0) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
313 A = zeros (sz, class (vals)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
314 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
315 A = repmat (fillval, sz); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
316 endif |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
317 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
318 ## Set the reduced values. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
319 A(subs) = vals; |
6770 | 320 endif |
321 endif | |
322 endfunction | |
323 | |
324 %!error (accumarray (1:5)) | |
325 %!error (accumarray ([1,2,3],1:2)) | |
326 %!assert (accumarray ([1;2;4;2;4],101:105), [101;206;0;208]) | |
327 %!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])) | |
328 %!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]))) | |
329 %!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])) | |
330 %!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]) | |
331 %!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)) | |
332 %!assert (accumarray ([1 1; 2 1; 2 3; 2 1; 2 3],1,[2,4]), [1,0,0,0;2,0,2,0]) | |
333 %!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]) | |
13788
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
334 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 0), [3; 4]) |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
335 %!assert (accumarray ([1; 2], [3; 4], [2, 1], @min, [], 1), sparse ([3; 4])) |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
336 %!assert (accumarray ([1; 2], [3; 4], [1, 2], @min, [], 0), [3, 4]) |
9ab64f063c96
Consistently allow creation of sparse vectors in accumarray
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13787
diff
changeset
|
337 %!assert (accumarray ([1; 2], [3; 4], [1, 2], @min, [], 1), sparse ([3, 4])) |
6770 | 338 %!test |
339 %! 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
|
340 %! assert (A{2},[102;104]) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
341 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
342 %! subs = ceil (rand (2000, 3)*10); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
343 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
344 %! assert (accumarray (subs, vals, [], @max), accumarray (subs, vals, [], @(x) max (x))); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
345 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
346 %! subs = ceil (rand (2000, 1)*100); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
347 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
348 %! assert (accumarray (subs, vals, [100, 1], @min, NaN), accumarray (subs, vals, [100, 1], @(x) min (x), NaN)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
349 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
350 %! subs = ceil (rand (2000, 2)*30); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
351 %! subsc = num2cell (subs, 1); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
352 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
353 %! assert (accumarray (subsc, vals, [], [], 0, true), accumarray (subs, vals, [], [], 0, true)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
354 %!test |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
355 %! subs = ceil (rand (2000, 3)*10); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
356 %! subsc = num2cell (subs, 1); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
357 %! vals = rand (2000, 1); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
358 %! assert (accumarray (subsc, vals, [], @max), accumarray (subs, vals, [], @max)); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
359 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
9859
diff
changeset
|
360 |