sum
Sum elements of an array.
Syntax
Section titled “Syntax”B = sum(A)B = sum(A, dim)B = sum(A, 'all')B = sum(A, dim, 'omitnan')B = sum(a1, a2, a3, ...) % Excel-compatible modeDescription
Section titled “Description”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.
Examples
Section titled “Examples”sum([1 2 3 4]) % 10sum(magic(3)) % [15 15 15] column sumssum(magic(3), 2) % [15; 15; 15] row sumssum(magic(3), 'all') % 45See also
Section titled “See also”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.