Main Content

sum

Sum of fi array elements

Description

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

  • If A is a vector, sum(A) returns the sum of the elements.

  • If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.

  • If A is 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) sums along the dimension dim of A.

example

S = sum(___,type) returns an array in the class specified by type.

example

Examples

collapse all

Create a fi vector and specify fimath properties in the constructor.

A =  fi([1 2 5 8 5], 'SumMode', 'KeepLSB', 'SumWordLength', 32)
A = 
     1     2     5     8     5

          DataTypeMode: Fixed-point: binary 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 of A.

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 output S is a scalar with the specified SumWordLength of 32. The FractionLength of S is 11 because SumMode was set to KeepLSB.

Create a fi array, 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 of A. The WordLength of S has increased by two bits because ceil(log2(size(A,1)))=2. The FractionLength remains the same because the default setting of SumMode is FullPrecision.

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

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. The WordLength of S is 18 because ceil(log2(size(A,2)))=2.

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

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
class(A)
ans = 
'embedded.fi'
S = sum(A, 'native')
S = 
     5    11    10

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 11
class(S)
ans = 
'embedded.fi'

MATLAB® preserves the data type of A and returns a row vector S of type embedded.fi.

Input Arguments

collapse all

fi input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fi
Complex Number Support: Yes

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

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

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

  • If type is 'double', then sum returns a double-precision array, regardless of the input data type.

  • If type is 'native', then sum returns an array with the same class as input array A.

Data Types: char

Output Arguments

collapse all

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

Note

The fimath object is used in the calculation of the sum. If SumMode is set to FullPrecision, KeepLSB, or KeepMSB, then the number of integer bits of growth for sum(A) is ceil(log2(size(A,dim))).

Limitations

  • sum does not support fi objects of data type Boolean.

Extended Capabilities

HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

Version History

Introduced before R2006a

See Also

| | | | | | | | |