sum - Sum of array elements - MATLAB (original) (raw)
Syntax
Description
S = sum([A](#btv6ok6-1-A))
returns the sum of the elements of A along the first array dimension whose size does not equal 1.
- 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. The size ofS
in this dimension becomes1
while the sizes of all other dimensions remain the same as inA
. - If
A
is a table or timetable, thensum(A)
returns a one-row table containing the sum of each variable. (since R2023a)
S = sum([A](#btv6ok6-1-A),`"all"`)
returns the sum of all elements of A
.
S = sum([A](#btv6ok6-1-A),[dim](#btv6ok6-1-dim))
returns the sum along dimension dim
. For example, ifA
is a matrix, then sum(A,2)
returns a column vector containing the sum of each row.
S = sum([A](#btv6ok6-1-A),[vecdim](#mw%5Fa0641061-edb0-4956-a9bd-949c6057ec15))
sums the elements of A
based on the dimensions specified in the vector vecdim
. For example, if A
is a matrix, then sum(A,[1 2])
returns the sum of all elements inA
because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.
S = sum(___,[outtype](#btv6ok6-1-outtype))
returns the sum with the specified data type, using any of the input arguments in the previous syntaxes. outtype
can be "default"
,"double"
, or "native"
.
S = sum(___,[nanflag](#mw%5F6b55b12b-fc8b-4e2f-888f-27cb854c0f2e))
specifies whether to include or omit NaN
values in A
. For example, sum(A,"omitnan")
ignores NaN
values when computing the sum. By default, sum
includesNaN
values.
Examples
Create a vector and compute the sum of its elements.
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×3
1 3 2
4 2 5
6 1 4
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×3
1 3 2
4 2 5
6 1 4
Use a vector dimension argument to operate on specific slices of an array.
Create a 3-D array whose elements are 1.
To sum all elements in each page of A
, 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 = S1(:,:,1) =
12
S1(:,:,2) =
12
If you slice A
along the first dimension, you can sum the elements of the resulting 4 pages, which are each 3-by-2 matrices.
Slicing along the second dimension, each page sum is over a 4-by-2 matrix.
To compute the sum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the "all"
option.
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)
Create a vector of 32-bit integers and compute the int32
sum of its elements by specifying the output type as native
.
A = int32(1:10); S = sum(A,"native")
Create a matrix containing NaN
values.
A = [1.77 -0.005 NaN -2.95; NaN 0.34 NaN 0.19]
A = 2×4
1.7700 -0.0050 NaN -2.9500
NaN 0.3400 NaN 0.1900
Compute the sum of the matrix, excluding NaN
values. For matrix columns that contain any NaN
value, sum
computes with the non-NaN
elements. For matrix columns that contain all NaN
values, the sum is 0.
S = 1×4
1.7700 0.3350 0 -2.7600
Input Arguments
Input array, specified as a vector, matrix, multidimensional array, table, or timetable.
- 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
| table
| timetable
Complex Number Support: Yes
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 whose size does not equal 1.
Dimension dim
indicates the dimension whose length reduces to 1. The size(S,dim)
is 1
, 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
returns A
when dim
is greater than ndims(A)
or when size(A,dim)
is 1
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
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 of A
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
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 is single,duration, table, or timetable, in which case, the output is "native" |
"double" | double, unless the data type is duration,table, ortimetable, in which case,"double" is not supported |
"native" | Same data type as the input, unless the input data type is char, in which case, "native" is not supported; or unless the input data type is timetable, in which case the output data type istable |
Missing value condition, specified as one of these values:
"includemissing"
or"includenan"
— IncludeNaN
values inA
when computing the sum. If any element in the operating dimension isNaN
, then the corresponding element inS
isNaN
."includemissing"
and"includenan"
have the same behavior."omitmissing"
or"omitnan"
— IgnoreNaN
values inA
, and compute the sum over fewer points. If all elements in the operating dimension areNaN
, then the corresponding element inS
is 0."omitmissing"
and"omitnan"
have the same behavior.
Extended Capabilities
Thesum
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- If used, the
dim
,vecdim
,outtype
, andnanflag
arguments must be constant at code generation time. - For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- If all dimensions of the input array are variable size at code generation time, this array cannot be empty at run time.
Usage notes and limitations:
- If you specify
dim
, then it must be a constant. - The
outtype
andnanflag
options must be constant character vectors.
The sum
function supports GPU array input with these 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
operation on the corresponding numeric array. The difference might be significant whenA
is a signed integer type and its product is accumulated natively.
For more information, see Run MATLAB Functions on a GPU (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
operation on the corresponding numeric array. The difference might be significant whenA
is a signed integer type and its product is accumulated natively.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Include or omit missing values in the input array when computing the sum by using the "includemissing"
or "omitmissing"
options. These options have the same behavior as the "includenan"
and"omitnan"
options, respectively.
The sum
function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
Operate on multiple dimensions of the input array at a time. Specify a vector of operating dimensions, or specify the "all"
option to operate on all array dimensions.