Add or subtract inputs
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations
The Sum block performs addition or subtraction on its inputs. The Add, Subtract, Sum of Elements, and Sum blocks are identical blocks. This block can add or subtract scalar, vector, or matrix inputs. It can also collapse the elements of a signal and perform a summation.
You specify the operations of the block with the List of signs
parameter with plus (+
), minus (
), and
spacer (
).
The number of +
and 
characters equals
the number of inputs. For example, ++
requires three inputs.
The block subtracts the second (middle) input from the first (top) input, and
then adds the third (bottom) input.
A spacer character creates extra space between ports on the block icon.
If performing only addition, you can use a numerical value equal to the number of inputs.
If only there is only one input port, a single +
or

adds or subtracts the elements over all dimensions or
in the specified dimension.
The Sum block first converts the input data type to its accumulator data type, then performs the specified operations. The block converts the result to its output data type using the specified rounding and overflow modes.
Output calculation for the Sum block depends on the number of block inputs and the sign of input ports:
If the Sum block has...  And...  The formula for output calculation is...  Where... 

One input port 
The input port sign is + 
y = e[0] + e[1] + e[2] ... + e[m] 

The input port sign is – 
y = 0.0 – e[0] – e[1] – e[2] ... – e[m]  
Two or more input ports 
All input port signs are – 
y = 0.0 – u[0] – u[1] – u[2] ... – u[n] 

The k^{th} input port is the first port where the sign is + 
y = u[k] – u[0] – u[1] – u[2] – u[k–1] (+/–) u[k+1] ... (+/–) u[n] 
The inputs can be of different data types, unless you select the Require all inputs to have the same data type parameter.
Port_1
— First input operand signalInput signal to the addition or subtraction operation. If there is only one input signal, then addition or subtraction is performed on the elements over all dimensions or the specified dimension.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Port_n
— n
th input operand signaln
th input signal to the operations. The number of
inputs matches the number of signs in the List of
signs parameter. The block applies the operations to the
inputs in the order listed. You can also use a numerical value equal to
the number of input ports as the List of signs
parameter. The block creates the input ports and applies addition to all
inputs. For example, if you assign 5
for the
List of signs parameter, the block creates
5
input ports and adds them together to produce
the output.
All nonscalar inputs must have the same dimensions. Scalar inputs are expanded to have the same dimensions as other inputs.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Port_1
— Output signalOutput signal resulting from addition and/or subtraction operations. The output signal has the same dimension as the input signals.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Icon shape
— Block icon shapeDesignate the icon shape of the block as rectangular or round.
For a rectangular block, the first input port is the top port. For a round Sum block, the first input port is the port closest to the 12 o'clock position going in a counterclockwise direction around the block. Similarly, other input ports appear in counterclockwise order around the block.
Block Parameter:
IconShape 
Type: character vector 
Values:
'rectangular' 
'round' 
Default:
'round' 
List of signs
— Operations performed on inputs++
(default)  +
 
 
 integer
Enter addition and subtraction operations performed on the inputs. An
input port is created for each operation. A spacer
(
) creates extra space between the input ports on
the block icon. Addition is the default operation. If you only want to
add the inputs, enter the number of input ports. The operations are
performed in the order listed.
When you enter only one element, the block enables the Sum
over parameter. For a single vector input,
+
or 
adds or subtracts the
elements over all dimensions or in the specified dimension.
You can manipulate the positions of the input ports on the block
by inserting spacers (
) between the signs in the
List of signs parameter. For
example, “++
” creates an extra
space between the second and third input ports.
Block Parameter:
Inputs 
Type: character vector 
Values:
'+'  '' 
  integer 
Default:
'++' 
Sum over
— Dimensions for operations on a single vector inputSelect the dimension over which the block performs the sumover operation.
For All dimensions, all input elements are
summed. When you select configuration parameter Use algorithms optimized for rowmajor array
layout, Simulink^{®} enables rowmajor algorithms for simulation. To generate
rowmajor code, set configuration parameter Array layout (Simulink Coder) to
Rowmajor
in addition to selecting
Use algorithms optimized for rowmajor array
layout. The columnmajor and rowmajor algorithms differ
only in the summation order. In some cases, due to different operation
order on the same data set, you might experience minor numeric
differences in the outputs of columnmajor and rowmajor
algorithms.
When you select Specified dimensions, another parameter Dimension appears. Choose the specific dimension for summing the vector input.
Enabled when you list only one sign in the List of signs parameter.
Block Parameter:
CollapseMode 
Type: character vector 
Values: 'All
dimensions'  'Specified
dimension' 
Default: 'All
dimensions' 
Dimension
— Dimension for summation on vector input1
(default)  integer
When you choose Specified dimension for the Sum over parameter, specify the dimension over which to perform the operation.
The block follows the same summation rules as the MATLAB^{®}
sum
function.
Suppose that you have a 2by3 matrix U.
Setting Dimension to 1
results in the output Y being computed
as:
$$Y={\displaystyle {\sum}_{i=1}^{2}U(i,j)}$$
Setting Dimension to 2
results in the output Y being computed
as:
$$Y={\displaystyle {\sum}_{j=1}^{3}U(i,j)}$$
If the specified dimension is greater than the dimension of the input, an error message appears.
Enabled when you choose Specified
dimension
for the Sum over
parameter.
Block Parameter:
CollapseDim 
Type: character vector 
Value:
integer 
Default:
'1' 
Sample time
— Specify sample time as a value other than 1
1
(default)  scalarSpecify the sample time as a value other than 1. For more information, see Specify Sample Time.
This parameter is not visible unless it is explicitly set to a value other than
1
. To learn more, see Blocks for Which Sample Time Is Not Recommended.
Block Parameter:
SampleTime 
Type: character vector 
Values: scalar 
Default:
'1' 
Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant.
Require all inputs to have the same data type
— Require that all inputs have the same data typeoff
(default)  on
Specify if input signals must all have the same data type. If you enable this parameter, then an error occurs during simulation if the input signal types are different.
Block Parameter:
InputSameDT 
Type: character vector 
Values:
'off'  'on' 
Default:
'off' 
Accumulator data type
— Data type of the accumulatorInherit: Inherit via internal
rule
(default)  Inherit: Same as first input
 double
 single
 int8
 uint8
 int16
 uint16
 int32
 uint32
 int64
 uint64
 fixdt(1,16)
 fixdt(1,16,0)
 fixdt(1,16,2^0,0)
 <data type expression>
Choose the data type of the accumulator. The type can be inherited,
specified directly, or expressed as a data type object such as
Simulink.NumericType
. When you choose
Inherit: Inherit via internal
rule
, Simulink chooses a data type to balance numerical accuracy,
performance, and generated code size, while taking into account the
properties of the embedded target hardware.
Block Parameter:
AccumDataTypeStr 
Type: character vector 
Values:
'Inherit: Inherit via internal
rule  'Inherit: Same as first
input' 
'double' 'single'
 'int8' 
'uint8' 
'int16' 
'uint16' ,
'int32' 
'uint32' 
'int64' 
'uint64' 
'fixdt(1,16)' 
'fixdt(1,16,0)' 
'fixdt(1,16,2^0,0)' 
'<data type
expression>' 
Default:
'Inherit: Inherit via internal
rule' 
Output minimum
— Minimum output value for range checking[]
(default)  scalarLower value of the output range that Simulink checks.
Simulink uses the minimum to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (see Signal Ranges and Enable Simulation Range Checking).
Automatic scaling of fixedpoint data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Simulink Coder).
Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.
Block Parameter:
OutMin 
Type: character vector 
Values: '[ ]' 
scalar 
Default: '[ ]' 
Output maximum
— Maximum output value for range checking[]
(default)  scalarUpper value of the output range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (see Signal Ranges and Enable Simulation Range Checking).
Automatic scaling of fixedpoint data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Simulink Coder).
Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.
Block Parameter:
OutMax 
Type: character vector 
Values: '[ ]' 
scalar 
Default: '[ ]' 
Output data type
— Specify the output data typeInherit: Inherit via internal
rule
(default)  Inherit: Keep MSB
 Inherit: Keep LSB
 Inherit: Inherit via back
propagation
 Inherit: Same as first input
 Inherit: Same as accumulator
 double
 single
 int8
 uint8
 int16
 uint16
 int32
 uint32
 int64
 uint64
 fixdt(1,16)
 fixdt(1,16,0)
 fixdt(1,16,2^0,0)
 <data type expression>
Choose the data type for the output. The type can be inherited,
specified directly, or expressed as a data type object such as
Simulink.NumericType
.
When you select an inherited option, the block behaves as follows:
Inherit: Inherit via internal
rule
—Simulink chooses a data type to balance numerical
accuracy, performance, and generated code size, while taking
into account the properties of the embedded target hardware.
The accumulator internal rule favors greater numerical
accuracy, possibly at the cost of less efficient
generated code. To get the same accuracy for the output,
set the output data type to Inherit:
Inherit same as accumulator
.
Inherit: Keep MSB
– Simulink chooses a data type that maintains the full
range of the operation, then reduces the precision of the
output to a size appropriate for the embedded target hardware.
For more efficient generated code, set the
Accumulator data type to
Inherit: Inherit via internal
rule
, and deselect the
Saturate on integer overflow
parameter.
This rule never produces overflows.
Inherit: Keep LSB
– Simulink chooses a data type that maintains the
precision of the operation, but reduces the range if the
full type does not fit on the embedded target hardware.
For more efficient generated code, set the
Accumulator data type to
Inherit: Inherit via internal
rule
, and deselect the
Saturate on integer overflow
parameter.
This rule can produce overflows.
If you change the embedded target settings, the data type selected by these internal rules might change. It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If the rules do not meet your specific needs for numerical accuracy or performance, use one of the following options:
Specify the output data type explicitly.
Use the simple choice of Inherit:
Same as first input
.
Explicitly specify a default data type such as
fixdt(1,32,16)
and then use the
FixedPoint Tool to propose data types for your
model. For more information, see fxptdlg
.
To specify your own inheritance rule, use
Inherit: Inherit via back
propagation
and then use a Data Type
Propagation block. Examples of how to use
this block are available in the Signal Attributes
library Data Type Propagation
Examples block.
Inherit: Inherit via back
propagation
— Use data type of the driving
block.
Inherit: Same as first input
—
Use data type of the first input signal.
Inherit: Inherit same as
accumulator
— Use data type of the
accumulator.
Block Parameter:
OutDataTypeStr 
Type: character vector 
Values: 'Inherit:
Inherit via internal rule 'Inherit: Keep
MSB' 'Inherit: Keep LSB' 
'Inherit: Inherit via back
propagation' 'Inherit: Same as first
input'  'Inherit: Same as
accumulator'  'double' 
'single'  'int8' 
'uint8'  'int16' 
'uint16' , 'int32' 
'uint32'  'int64' 
'uint64' 'fixdt(1,16)'
 'fixdt(1,16,0)' 
'fixdt(1,16,2^0,0)'  '<data
type expression>' 
Default: 'Inherit:
Inherit via internal rule' 
Lock data type settings against changes by the fixedpoint tools
— Prevent fixedpoint tools from overriding data typesoff
(default)  on
Select to lock data type settings of this block against changes by the FixedPoint Tool and the FixedPoint Advisor. For more information, see Lock the Output Data Type Setting (FixedPoint Designer).
Block Parameter:
LockScale 
Values:
'off'  'on' 
Default:
'off' 
Integer rounding mode
— Rounding mode for fixedpoint operationsFloor
(default)  Ceiling
 Convergent
 Nearest
 Round
 Simplest
 Zero
Specify the rounding mode for fixedpoint operations. For more information, see Rounding (FixedPoint Designer).
Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression using a MATLAB rounding function into the mask field.
Block Parameter:
RndMeth 
Type: character vector 
Values:
'Ceiling'  'Convergent'  'Floor'  'Nearest'  'Round'  'Simplest' 
'Zero' 
Default:
'Floor' 
Saturate on integer overflow
— Method of overflow actionoff
(default)  on
Specify whether overflows saturate or wrap.
Action  Rationale  Impact on Overflows  Example 

Select this check box ( 
Your model has possible overflow, and you want explicit saturation protection in the generated code. 
Overflows saturate to either the minimum or maximum value that the data type can represent. 
The maximum value that the 
Do not select this check box ( 
You want to optimize efficiency of your generated code. You want to avoid overspecifying how a block handles outofrange signals. For more information, see Check for Signal Range Errors. 
Overflows wrap to the appropriate value that is representable by the data type. 
The maximum value that the 
When you select this check box, saturation applies to every internal operation on the block, not just the output, or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.
Block Parameter: SaturateOnIntegerOverflow 
Type: character vector 
Values:
'off'  'on' 
Default: 'off' 
Data Types 

Direct Feedthrough 

Multidimensional Signals 

VariableSize Signals 

ZeroCrossing Detection 

For more information on HDL code generation, see Add.
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.
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: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.