MATLAB Function Reference
  Go to function:
    Search    Help Desk 
sum    Examples   See Also

Sum of array elements

Syntax

Description

B = sum(A) returns sums along different dimensions of an array.

If A is a vector, sum(A) returns the sum of the elements.

If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.

If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.

B = sum(A,dim) sums along the dimension of A specified by scalar dim.

Remarks

sum(diag(X)) is the trace of X.

Examples

The magic square of order 3 is

This is called a magic square because the sums of the elements in each column are the same.

as are the sums of the elements in each row, obtained by transposing:

See Also

cumsum      Cumulative sum

diff        Differences and approximate derivatives

prod        Product of array elements

trace       Sum of diagonal elements



[ Previous | Help Desk | Next ]