Skip to content

sum

Sum elements of an array.

B = sum(A)
B = sum(A, dim)
B = sum(A, 'all')
B = sum(A, dim, 'omitnan')
B = sum(a1, a2, a3, ...) % Excel-compatible mode

Returns the sum of A’s elements. With no dimension argument, sums along the first non-singleton dimension. With dim, sums along that dimension. With the string 'all', sums every element to a single scalar.

String flags accepted in any position: 'omitnan' (default — NaN values skipped) vs. 'includenan'; 'double' / 'default' / 'native' to control output element type.

In Excel-compatible mode (cell formulas), sum is variadic: it accepts any number of cell ranges or scalars and returns the total, ignoring NaN/empty cells. Same as Excel’s SUM().

When A is a graph handle, returns the sum of the plotted data points.

sum([1 2 3 4]) % 10
sum(magic(3)) % [15 15 15] column sums
sum(magic(3), 2) % [15; 15; 15] row sums
sum(magic(3), 'all') % 45
  • prod — Product of array elements.
  • cumsum — Cumulative sum along a dimension.
  • mean — Arithmetic mean.
  • all — Test whether all elements of an array are nonzero.
  • any — Test whether any element of an array is nonzero.

Also categorized under: Range Functions / Math.