sum
Sum of array elements.
📝Syntax
R = sum(M)
R = sum(M, d)
R = sum(M, 'all')
R = sum(M, ___, f)
R = sum(M, d, t)
R = sum(M, 'all', t, f)
📥Input Arguments
Parameter Description
M an array of double, single, integers, ...
d dimension to operate along: positive integer scalar.
'all' sum all elements of M and return a scalar.
t a string: 'default', 'double' or 'native'.
f a string: 'includenan' or 'omitnan'.
📤Output Arguments
Parameter Description
R Sum of array elements.
📄Description

R = sum(M) returns the sum along the first non-singleton dimension of M.

R = sum(M, d) sums along dimension d. R = sum(M, 'all') sums all elements of M and returns a scalar.

Optional text arguments control the output type ('default', 'double' or 'native') and NaN handling ('includenan' or 'omitnan').

💡Examples
Sum along a dimension.
M = [1 2; 3 4];
R = sum(M, 2)
Sum all elements.
M = [1 2; 3 4];
R = sum(M, 'all')
Keep the native integer output type.
M = uint8([10:30:70;20:30:80;30:30:90]);
R = sum(M, 'native')
🔗See Also
ndimsprod
🕔Version History
Version Description
1.0.0 initial version
Edit this page on GitHub