comparison scripts/general/accumarray.m @ 13153:25effffba9b0

maint: Periodic merge of stable to default
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 17 Sep 2011 22:07:41 -0500
parents cefd568ea073 8d5f0b41e6b0
children 8aaaef4a69aa
comparison
equal deleted inserted replaced
13148:54f804016276 13153:25effffba9b0
17 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{func}, @var{fillval}, @var{issparse}) 21 ## @deftypefn {Function File} {} accumarray (@var{subs}, @var{vals}, @var{sz}, @var{func}, @var{fillval}, @var{issparse})
22 ## @deftypefnx {Function File} {} accumarray (@var{csubs}, @var{vals}, @dots{}) 22 ## @deftypefnx {Function File} {} accumarray (@var{subs}, @var{vals}, @dots{})
23 ## 23 ##
24 ## Create an array by accumulating the elements of a vector into the 24 ## Create an array by accumulating the elements of a vector into the
25 ## positions defined by their subscripts. The subscripts are defined by 25 ## positions defined by their subscripts. The subscripts are defined by
26 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each row 26 ## the rows of the matrix @var{subs} and the values by @var{vals}. Each
27 ## of @var{subs} corresponds to one of the values in @var{vals}. 27 ## row of @var{subs} corresponds to one of the values in @var{vals}. If
28 ## 28 ## @var{vals} is a scalar, it will be used for each of the row of
29 ## The size of the matrix will be determined by the subscripts themselves. 29 ## @var{subs}.
30 ## However, if @var{sz} is defined it determines the matrix size. The length 30 ##
31 ## of @var{sz} must correspond to the number of columns in @var{subs}. 31 ## The size of the matrix will be determined by the subscripts
32 ## 32 ## themselves. However, if @var{sz} is defined it determines the matrix
33 ## The default action of @code{accumarray} is to sum the elements with the 33 ## size. The length of @var{sz} must correspond to the number of columns
34 ## same subscripts. This behavior can be modified by defining the @var{func} 34 ## in @var{subs}.
35 ## function. This should be a function or function handle that accepts a 35 ##
36 ## column vector and returns a scalar. The result of the function should not 36 ## The default action of @code{accumarray} is to sum the elements with
37 ## depend on the order of the subscripts. 37 ## the same subscripts. This behavior can be modified by defining the
38 ## 38 ## @var{func} function. This should be a function or function handle
39 ## The elements of the returned array that have no subscripts associated with 39 ## that accepts a column vector and returns a scalar. The result of the
40 ## them are set to zero. Defining @var{fillval} to some other value allows 40 ## function should not depend on the order of the subscripts.
41 ## these values to be defined. 41 ##
42 ## 42 ## The elements of the returned array that have no subscripts associated
43 ## By default @code{accumarray} returns a full matrix. If @var{issparse} is 43 ## with them are set to zero. Defining @var{fillval} to some other
44 ## logically true, then a sparse matrix is returned instead. 44 ## value allows these values to be defined.
45 ## 45 ##
46 ## An example of the use of @code{accumarray} is: 46 ## By default @code{accumarray} returns a full matrix. If
47 ## @var{issparse} is logically true, then a sparse matrix is returned
48 ## instead.
49 ##
50 ## The following @code{accumarray} example constructs a frequency table
51 ## that in the first column counts how many occurrences each number in
52 ## the second column has, taken from the vector @var{x}. Note the usage
53 ## of @code{unique} for assigning to all repeated elements of @var{x}
54 ## the same index (@xref{doc-unique}).
47 ## 55 ##
48 ## @example 56 ## @example
49 ## @group 57 ## @group
50 ## accumarray ([1,1,1;2,1,2;2,3,2;2,1,2;2,3,2], 101:105) 58 ## x = [91, 92, 90, 92, 90, 89, 91, 89, 90, 100, 100, 100];
59 ## [u, ~, j] = unique (x);
60 ## [accumarray(j', 1), u']
61 ## @result{} 2 89
62 ## 3 90
63 ## 2 91
64 ## 2 92
65 ## 3 100
66 ## @end group
67 ## @end example
68 ##
69 ## Another example, where the result is a multidimensional 3D array and
70 ## the default value (zero) appears in the output:
71 ##
72 ## @example
73 ## @group
74 ## accumarray ([1, 1, 1;
75 ## 2, 1, 2;
76 ## 2, 3, 2;
77 ## 2, 1, 2;
78 ## 2, 3, 2], 101:105)
51 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0] 79 ## @result{} ans(:,:,1) = [101, 0, 0; 0, 0, 0]
52 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208] 80 ## ans(:,:,2) = [0, 0, 0; 206, 0, 208]
53 ## @end group 81 ## @end group
54 ## @end example 82 ## @end example
55 ## 83 ##
56 ## The complexity in the non-sparse case is generally O(M+N), where N is the 84 ## The complexity in the non-sparse case is generally O(M+N), where N is
57 ## number of 85 ## the number of subscripts and M is the maximum subscript (linearized
58 ## subscripts and M is the maximum subscript (linearized in multi-dimensional 86 ## in multi-dimensional case). If @var{func} is one of @code{@@sum}
59 ## case). 87 ## (default), @code{@@max}, @code{@@min} or @code{@@(x) @{x@}}, an
60 ## If @var{func} is one of @code{@@sum} (default), @code{@@max}, @code{@@min} 88 ## optimized code path is used. Note that for general reduction function
61 ## or @code{@@(x) @{x@}}, an optimized code path is used. 89 ## the interpreter overhead can play a major part and it may be more
62 ## Note that for general reduction function the interpreter overhead can play a 90 ## efficient to do multiple accumarray calls and compute the results in
63 ## major part and it may be more efficient to do multiple accumarray calls and 91 ## a vectorized manner.
64 ## compute the results in a vectorized manner. 92 ##
65 ## @seealso{accumdim} 93 ## @seealso{accumdim, unique}
66 ## @end deftypefn 94 ## @end deftypefn
67 95
68 function A = accumarray (subs, vals, sz = [], func = [], fillval = [], issparse = []) 96 function A = accumarray (subs, vals, sz = [], func = [], fillval = [], issparse = [])
69 97
70 if (nargin < 2 || nargin > 6) 98 if (nargin < 2 || nargin > 6)