[TOC]

sum

Sum of elements in an array

Usages

s = sum(A)

s = sum(A, k)

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

Examples

Input
a = [1 2 3; 1 2 3; 1 2 3];
% Creating a 3D array by repeating a matrix along the 3rd dimension.
b = repmat(a, [1, 1, 3])
% The sum along the 3rd dimension.
sum(b, 3)
Output
b(:, :, 1) = 
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000

b(:, :, 2) = 
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000

b(:, :, 3) = 
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000
 1.0000   2.0000   3.0000

ans = 
 3.0000   6.0000   9.0000
 3.0000   6.0000   9.0000
 3.0000   6.0000   9.0000