sum
Sum of array elements
Syntax
Description
S = sum(
returns the sum of the elements of A along the first array dimension whose size does not equal 1.A
)
If
A
is a vector, thensum(A)
returns the sum of the elements.If
A
is a matrix, thensum(A)
returns a row vector containing the sum of each column.If
A
is a multidimensional array, thensum(A)
operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes1
while the sizes of all other dimensions remain the same.
S = sum(
computes the sum of all elements ofA
,'all'
)A
. This syntax is valid for MATLAB®versions R2018b and later.
S = sum(___,
returns the sum with a specified data type, using any of the input arguments in the previous syntaxes.outtype
)outtype
can be'default'
,'double'
, or'native'
.
Examples
Sum of Vector Elements
Sum of Matrix Columns
Create a matrix and compute the sum of the elements in each column.
A = [1 3 2; 4 2 5; 6 1 4]
A =3×31 3 2 4 2 5 6 1 4
S = sum(A)
S =1×311 6 11
Sum of Matrix Rows
Create a matrix and compute the sum of the elements in each row.
A = [1 3 2; 4 2 5; 6 1 4]
A =3×31 3 2 4 2 5 6 1 4
S = sum(A,2)
S =3×16 11 11
Sum of Array Slices
Use a vector dimension argument to operate on specific slices of an array.
Create a 3-D array whose elements are 1.
A = ones(4,3,2);
To sum all elements in each page ofA
, specify the dimensions in which to sum (row and column) using a vector dimension argument. Since both pages are a 4-by-3 matrix of ones, the sum of each page is 12.
S1 = sum(A,[1 2])
S1 = S1(:,:,1) = 12 S1(:,:,2) = 12
If you sliceA
along the first dimension, you can sum the elements of the resulting 4 pages, which are each 3-by-2 matrices.
S2 = sum(A,[2 3])
S2 =4×16 6 6 6
Slicing along the second dimension, each page sum is over a 4-by-2 matrix.
S3 = sum(A,[1 3])
S3 =1×38 8 8
Starting in R2018b, to sum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the'all'
option.
S4 = sum(A,[1 2 3])
S4 = 24
Sall = sum(A,'all')
Sall = 24
Sum of 3-D Array
Create a 4-by-2-by-3 array of ones and compute the sum along the third dimension.
A = ones(4,2,3); S = sum(A,3)
S =4×23 3 3 3 3 3 3 3
Sum of 32-bit Integers
Create a vector of 32-bit integers and compute theint32
sum of its elements by specifying the output type asnative
.
A = int32(1:10); S = sum(A,'native')
S =int3255
Sum ExcludingNaN
Create a vector and compute its sum, excludingNaN
values.
A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; S = sum(A,'omitnan')
S = 3.3250
If you do not specify'omitnan'
, thensum(A)
returnsNaN
.
Input Arguments
A
—Input array
vector|matrix|multidimensional array
Input array, specified as a vector, matrix, or multidimensional array.
If
A
is a scalar, thensum(A)
returnsA
.If
A
is an empty 0-by-0 matrix, thensum(A)
returns0
.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
|char
|duration
Complex Number Support:Yes
dim
—Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.
Dimensiondim
indicates the dimension whose length reduces to 1. Thesize(S,dim)
is1
, while the sizes of all other dimensions remain the same.
Consider a two-dimensional input array,A
:
sum(A,1)
operates on successive elements in the columns ofA
and returns a row vector of the sums of each column.sum(A,2)
operates on successive elements in the rows ofA
and returns a column vector of the sums of each row.
sum
returnsA
whendim
is greater thanndims(A)
or whensize(A,dim)
is1
.
Data Types:double
|single
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
vecdim
—Vector of dimensions
vector of positive integers
Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.
Consider a 2-by-3-by-3 input array,A
. Thensum(A,[1 2])
returns a 1-by-1-by-3 array whose elements are the sums of each page ofA
.
Data Types:double
|single
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
outtype
—Output data type
'default'
(default) |'double'
|'native'
Output data type, specified as'default'
,'double'
, or'native'
. These options also specify the data type in which the operation is performed.
outtype |
Output data type |
---|---|
'default' |
double , unless the input data type issingle orduration , in which case, the output is'native' |
'double' |
double , unless the data type isduration , in which case,'double' is not supported |
'native' |
same data type as the input, unless the input data type ischar , in which case,'native' is not supported |
Data Types:char
nanflag
—NaN
condition
‘includenan
’(default) |‘omitnan
’
NaN
condition, specified as one of these values:
'includenan'
— IncludeNaN
values when computing the sum, resulting inNaN
.'omitnan'
— Ignore allNaN
values in the input.
Data Types:char
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, seeTall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If you specify
dim
, then it must be a constant.The
outtype
andnanflag
options must be constant character vectors.SeeVariable-Sizing Restrictions for Code Generation of Toolbox Functions(MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
If you specify
dim
, then it must be a constant.The
outtype
andnanflag
options must be constant character vectors.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
The order of the additions in the
sum
operation is not defined. Therefore, thesum
operation on a GPU array might not return exactly the same answer as thesum
操作相应的MATLAB数值array. The difference might be significant whenA
is a signed integer type and its product is accumulated natively.
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
The order of the additions in
sum
operation is not defined. Therefore, thesum
operation on a distributed array might not return exactly the same answer as thesum
操作相应的MATLAB数值array. The difference might be significant whenA
is a signed integer type and its product is accumulated natively.
For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)