Skip to content

sum

Sum of numbers

S = sum(A)

  • If A is a scalar, S is equal to A. If A is a vector, S is the sum of elements along the longer dimension of A.
  • If A is not a vector, each element of S is the sum of all elements of the corresponding vector of A along the first non-singleton dimension.

S = sum(A, k)

  • This is the same as S = sum(A), except that each element of S corresponds to a vector of A along its k-th dimension.
  • k should be less than or equal to the number of dimensions of A.

Note

When adding up two numbers by sum, if any one of them is NaN, the result will be NaN.