sum

Purpose

Sum of elements.

Synopsis

y = sum(X)

Description

y = sum(X), where X is a matrix, returns a row y vector with the sum over each column. If X is a vector, y is the sum of the elements of X.

Examples

The magic square of order 3 is

M = magic(3) 
          
M = 
     8    1    6
     3    5    7
     4    9    2
This is called a magic square because the sum of the elements in each column is the same.

sum(M) =
     15    15    15
And the sum of the elements in each row, which can be obtained by transposing twice, is the same.

sum(M')' = 
     15
     15
     15

See Also

cumsum, prod, trace

(c) Copyright 1994 by The MathWorks, Inc.