### 17.4 Sums and Products

— Built-in Function: sum (x)
— Built-in Function: sum (x, dim)
— Built-in Function: sum (..., 'native')
— Built-in Function: sum (..., 'double')
— Built-in Function: sum (..., 'extra')

> Sum of elements along dimension dim. If dim is omitted, it defaults to the first non-singleton dimension.

> If the optional argument 'native' is given, then the sum is performed in the same type as the original argument, rather than in the default double type. For example:

>               sum ([true, true])
>                 ⇒ 2
>               sum ([true, true], 'native')
>                 ⇒ true

> On the contrary, if 'double' is given, the sum is performed in double precision even for single precision inputs.

> For double precision inputs, 'extra' indicates that a more accurate algorithm than straightforward summation is to be used. For single precision inputs, 'extra' is the same as 'double'. Otherwise, 'extra' has no effect.

> See also: cumsum, sumsq, prod.

— Built-in Function: prod (x)
— Built-in Function: prod (x, dim)

> Product of elements along dimension dim. If dim is omitted, it defaults to the first non-singleton dimension.

> See also: cumprod, sum.

— Built-in Function: cumsum (x)
— Built-in Function: cumsum (x, dim)
— Built-in Function: cumsum (..., 'native')
— Built-in Function: cumsum (..., 'double')
— Built-in Function: cumsum (..., 'extra')

> Cumulative sum of elements along dimension dim. If dim is omitted, it defaults to the first non-singleton dimension.

> See `sum` for an explanation of the optional parameters 'native', 'double', and 'extra'.

> See also: sum, cumprod.

— Built-in Function: cumprod (x)
— Built-in Function: cumprod (x, dim)

> Cumulative product of elements along dimension dim. If dim is omitted, it defaults to the first non-singleton dimension.

> See also: prod, cumsum.

— Built-in Function: sumsq (x)
— Built-in Function: sumsq (x, dim)

> Sum of squares of elements along dimension dim. If dim is omitted, it defaults to the first non-singleton dimension.

> This function is conceptually equivalent to computing

>               sum (x .* conj (x), dim)

> but it uses less memory and avoids calling `conj` if x is real.

> See also: sum.

— Function File: accumarray (subs, vals, sz, func, fillval, issparse)
— Function File: accumarray (subs, vals, ...)

> Create an array by accumulating the elements of a vector into the positions defined by their subscripts. The subscripts are defined by the rows of the matrix subs and the values by vals. Each row of subs corresponds to one of the values in vals. If vals is a scalar, it will be used for each of the row of subs.

> The size of the matrix will be determined by the subscripts themselves. However, if sz is defined it determines the matrix size. The length of sz must correspond to the number of columns in subs.

> The default action of `accumarray` is to sum the elements with the same subscripts. This behavior can be modified by defining the func function. This should be a function or function handle that accepts a column vector and returns a scalar. The result of the function should not depend on the order of the subscripts.

> The elements of the returned array that have no subscripts associated with them are set to zero. Defining fillval to some other value allows these values to be defined.

> By default `accumarray` returns a full matrix. If issparse is logically true, then a sparse matrix is returned instead.

> The following `accumarray` example constructs a frequency table that in the first column counts how many occurrences each number in the second column has, taken from the vector x. Note the usage of `unique` for assigning to all repeated elements of x the same index (see doc-unique).

>               x = [91, 92, 90, 92, 90, 89, 91, 89, 90, 100, 100, 100];
>               [u, ~, j] = unique (x);
>               [accumarray(j', 1), u']
>               ⇒ 2    89
>                  3    90
>                  2    91
>                  2    92
>                  3   100

> Another example, where the result is a multidimensional 3D array and the default value (zero) appears in the output:

>               accumarray ([1, 1, 1;
>                            2, 1, 2;
>                            2, 3, 2;
>                            2, 1, 2;
>                            2, 3, 2], 101:105)
>               ⇒ ans(:,:,1) = [101, 0, 0; 0, 0, 0]
>                  ans(:,:,2) = [0, 0, 0; 206, 0, 208]

> The complexity in the non-sparse case is generally O(M+N), where N is the number of subscripts and M is the maximum subscript (linearized in multi-dimensional case). If func is one of `@sum` (default), `@max`, `@min` or `@(x) {x}`, an optimized code path is used. Note that for general reduction function the interpreter overhead can play a major part and it may be more efficient to do multiple accumarray calls and compute the results in a vectorized manner.

> See also: accumdim, unique.

— Function File: accumdim (subs, vals, dim, n, func, fillval)

> Create an array by accumulating the slices of an array into the positions defined by their subscripts along a specified dimension. The subscripts are defined by the index vector subs. The dimension is specified by dim. If not given, it defaults to the first non-singleton dimension.

> The extent of the result matrix in the working dimension will be determined by the subscripts themselves. However, if n is defined it determines this extent.

> The default action of `accumdim` is to sum the subarrays with the same subscripts. This behavior can be modified by defining the func function. This should be a function or function handle that accepts an array and a dimension, and reduces the array along this dimension. As a special exception, the built-in `min` and `max` functions can be used directly, and `accumdim` accounts for the middle empty argument that is used in their calling.

> The slices of the returned array that have no subscripts associated with them are set to zero. Defining fillval to some other value allows these values to be defined.

> An example of the use of `accumdim` is:

>               accumdim ([1, 2, 1, 2, 1], [7,-10,4;-5,-12,8;-12,2,8;-10,9,-3;-5,-3,-13])
>               ⇒ ans = [-10,-11,-1;-15,-3,5]

> See also: accumarray.