Written and maintained by CASRAI Editorial Board
Published
On this page
If you need to add up numbers in MATLAB, sum(A) does it: it returns the sum of a vector’s elements, or a row vector of column sums for a matrix. This page covers the whole call: sum by column, row or every element, sum ignoring NaN, and sum from 1 to n numerically and symbolically. All outputs below are the examples in MathWorks’ sum reference (read 2026-11-08).
What MATLAB sum is used for in research
Analysts use sum to total counts, accumulate residuals, collapse a dimension of a measurement array (for example, summing over trials or channels), and compute the numerator of a mean. It is a base MATLAB function. The related symsum, used for sums with a symbolic index, belongs to the Symbolic Math Toolbox. If you are new to scripts and functions, start with MATLAB coding: scripts, functions and a first program and MATLAB code.
Install or access
MATLAB is a MathWorks product, so access and licensing come from MathWorks or your institution’s MATLAB licence. The documentation for sum is on the MathWorks help site (see Sources). symsum requires the Symbolic Math Toolbox in addition to MATLAB.
Step by step: sum a vector, a matrix and an array
Sum in MATLAB: the syntaxes
The reference lists six forms: S = sum(A), S = sum(A,"all"), S = sum(A,dim), S = sum(A,vecdim), S = sum(___,outtype) and S = sum(___,nanflag).
- Sum a vector.
A = 1:10; S = sum(A)returnsS = 55. - Sum a matrix by column.
A = [1 3 2; 4 2 5; 6 1 4]; S = sum(A)returns the 1×3 row vector11 6 11. For an N-D array,sum(A)works along the first dimension whose size is not 1. - Sum a matrix by row.
sum(A,2)on the same matrix returns the column vector[6; 11; 11].dim1 gives column sums,dim2 gives row sums. - Sum every element.
sum(A,"all")returns one number for the whole array. WithA = ones(4,3,2)it returns 24. - Sum over several dimensions. With
A = ones(4,3,2):sum(A,[1 2])gives 12 on each of the two pages (a 1x1x2 result),sum(A,[2 3])gives a 4×1 column of 6, andsum(A,[1 3])gives8 8 8. - Sum along the third dimension. With
A = ones(4,2,3),sum(A,3)returns a 4×2 matrix of 3s.
Choosing the right call
| What you want | Command | Result in the documented example |
|---|---|---|
| Total of a vector | sum(1:10) |
55 |
| Sum by column | sum(A) or sum(A,1) |
11 6 11 |
| Sum by row | sum(A,2) |
[6; 11; 11] |
| Every element of an array | sum(A,"all") |
24 for ones(4,3,2) |
| Each page of a 3-D array | sum(A,[1 2]) |
12 on each page |
| Ignore NaN values | sum(A,"omitnan") |
1.7700 0.3350 0 -2.7600 |
| Keep the integer class | sum(int32(1:10),"native") |
int32 55 |
| Symbolic sum from 1 to n | syms k n; symsum(k,k,1,n) |
n(n+1)/2 |
Output and how to read it
Sum ignoring NaN
By default ("includenan") a NaN anywhere in the summed slice makes that result NaN. "omitnan" ignores NaN values, and "omitmissing" behaves the same. With A = [1.77 -0.005 NaN -2.95; NaN 0.34 NaN 0.19], sum(A,"omitnan") returns 1.7700 0.3350 0 -2.7600. Note the third column: it holds only NaN, and the documented result there is 0, not NaN. If you report totals from data with missing values, a 0 from an all-missing column looks the same as a true zero total, so count the non-missing values separately.
Output class
The outtype option controls the class of the result. "default" returns double, except that single, duration, table and timetable inputs return their native type; "double" forces double; "native" returns the input class unless the input is char. Example: sum(int32(1:10),"native") returns int32 55. For floating-point behaviour in general see the IEEE 754 floating-point standard.
Sum from 1 to n (summation notation)
Numerically, sum(1:n) adds the integers 1 to n for a specific n, and it replaces a for-loop accumulator: build the vector, then call sum on it (editorial note: this is the usual vectorised alternative to a loop). When you want a formula rather than a number, use symsum from the Symbolic Math Toolbox. Per the MathWorks symsum page (read 2026-11-08), symsum(f,k,a,b) is a definite sum and symsum(f,k) an indefinite one. With syms k n, symsum(k,k,1,n) returns n(n+1)/2, symsum(1/k^2,k,1,Inf) returns π²/6, and symsum(x^k/factorial(k),k,1,Inf) returns e^x - 1 (with syms x k).
Troubleshooting
- The result is NaN. At least one NaN is in the summed slice and the default is
"includenan". Add"omitnan". - You got a row of sums when you wanted one number. For a matrix,
sum(A)sums columns. Usesum(A,"all")for one total orsum(A,2)for row sums. - The result is the wrong class. Set
outtypeto"double"or"native"deliberately. symsumis not recognised. It belongs to the Symbolic Math Toolbox; declare symbols withsymsfirst.
Related array functions: max in MATLAB, diff in MATLAB and repmat in MATLAB.
Free alternatives
| Tool | Equivalent call | Documented behaviour |
|---|---|---|
| GNU Octave | sum (x), sum (x, dim), sum (x, "all") |
Same dim, vecdim, “all”, outtype and nanflag forms; a matrix gives column sums; "omitnan" gives 0 for an all-NaN slice (Octave manual, read 2026-11-08). |
| NumPy (Python) | numpy.sum(a, axis=None, ...) |
The default axis=None sums all elements; axis may be an int or tuple of ints (NumPy documentation, read 2026-11-08). This default differs from MATLAB’s column-wise default for matrices. |
FAQ
How do I sum a matrix by column or by row in MATLAB?
sum(A) or sum(A,1) sums each column; sum(A,2) sums each row.
How do I sum all elements of a matrix?
sum(A,"all"), or sum(A,[1 2]) for a 2-D array.
How do I sum from 1 to n in MATLAB?
Numerically use sum(1:n). Symbolically, with the Symbolic Math Toolbox, symsum(k,k,1,n) returns n(n+1)/2.
Does sum ignore NaN?
Not by default. Use sum(A,"omitnan"); an all-NaN slice then returns 0.
What is summation in MATLAB with a for loop?
You can accumulate in a loop, but sum on a vector gives the same total in one call. For symbolic or infinite series use symsum.
Sources
- MathWorks, sum: Sum of elements, read 2026-11-08.
- MathWorks, symsum: Symbolic summation, read 2026-11-08.
- GNU Octave manual, Sums and Products, read 2026-11-08.
- NumPy, numpy.sum, read 2026-11-08.







