Language Reference


SUM Function

SUM (matrix1 <, matrix2, …, matrix15> );

The SUM function returns as a single numeric value the sum of all the elements in all arguments. There can be as many as 15 argument matrices. The SUM function checks for missing values and does not include them in the summation. It returns 0 if all values are missing.

For example, following statements compute the sum of all elements in the matrix A:

a = {2 1 ., 0 -1 0};
b = sum(a);
print b;

Figure 25.406: Sum of Matrix Elements

b
2



If you want to compute the sum for each row or for each column of a matrix, you can use the subscript reduction operator, as follows:

  • a[+,] computes a $1 \times 3$ row vector that contains the sum of each column.

  • a[,+] computes a $2 \times 1$ column vector that contains the sum of each row.

  • a[+] computes a scalar value that is equivalent to sum(a).

See the section Subscript Reduction Operators for more information about subscript reduction operators.