Documentation

sum

Sum of array elements

Syntax

S= sum (A)
S= sum ( A, dim)
S = sum (___, type )

Description

example

S= sum (A)returns the sum along different dimensions of thefiarrayA.

IfAis a vector,sum(A)returns the sum of the elements.

IfAis a matrix,sum(A)treats the columns ofAas vectors, returning a row vector of the sums of each column.

IfAis a multidimensional array,sum(A)treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.

example

S= sum (A,dim)沿着维度总结dimofA.

example

S= sum (___,type)returns an array in the class specified bytype, using any of the input arguments in the previous syntaxes.typecan be'double'or'native'.

  • Iftypeis'double', thensumreturns a double-precision array, regardless of the input data type.

  • Iftypeis'native', thensumreturns an array with the same class of input arrayA.

Thefimathobject is used in the calculation of the sum. IfSumModeisFullPrecision,KeepLSB, orKeepMSB, then the number of integer bits of growth forsum(A)isceil(log2(size(A,dim))).

sumdoes not supportfiobjects of data typeBoolean.

Examples

collapse all

Create afivector, and specifyfimathproperties in the constructor.

A=fi([1 2 5 8 5],'SumMode','KeepLSB','SumWordLength', 32)
5 = 1 2 5 8 DataTypeMode:定点:二进制point scaling Signedness: Signed WordLength: 16 FractionLength: 11 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: KeepLSB SumWordLength: 32 CastBeforeSum: true

Compute the sum of the elements ofA.

S=sum(A)
S = 21 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 11 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: KeepLSB SumWordLength: 32 CastBeforeSum: true

The outputSis a scalar with the specifiedSumWordLengthof 32. TheFractionLengthofSis 11 becauseSumModewas set toKeepLSB.

Create afiarray, and compute the sum of the elements in each column.

A=fi([1 2 8;3 7 0;1 2 2])
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
S=sum(A)
S = 5 11 10 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11

MATLAB® returns a row vector with the sums of each column ofA. TheWordLengthofShas increased by two bits becauseceil(log2(size(A,1)))=2. TheFractionLengthremains the same because the default setting ofSumModeisFullPrecision.

Compute the sum along the second dimension (dim=2) of 3-by-3 matrixA.

A=fi([1 2 8;3 7 0;1 2 2])
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
S=sum(A, 2)
S = 11 10 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11

MATLAB® returns a column vector of the sums of the elements in each row. TheWordLengthofSis 18 becauseceil(log2(size(A,2)))=2.

Compute the sums of the columns ofAso that the output array,S, has the same data type.

A=fi([1 2 8;3 7 0;1 2 2]), class(A)
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
答= 'embedded.fi'
S=sum(A,'native'), class(S)
S = 5 11 10 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11
答= 'embedded.fi'

MATLAB® preserves the data type ofAand returns a row vectorSof typeembedded.fi.

Input Arguments

collapse all

fiinput array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types:fi|single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Complex Number Support: Yes

Dimension to operate along, specified as a positive integer scalar.dimcan also be afiobject. If no value is specified, the default is the first array dimension whose size does not equal 1.

Data Types:fi|single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output class, specified as'double'or'native', defines the data type that the operation is performed in and returned in.

Data Types:char

Output Arguments

collapse all

Sum array, returned as a scalar, vector, matrix, or multidimensional array.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?