Cumulative Sum
Cumulative sum of channel, column, or row elements
Libraries:
DSP System Toolbox /
Math Functions /
Math Operations
Description
The Cumulative Sum block computes the cumulative sum along the specified dimension of the input or across time (running sum).
Ports
Input
In — Input signal
vector  matrix
Input, specified as a vector or as matrix inputs containing real or complex values.
This port is unnamed until you select a
nonNone
value for the Reset
port
parameter.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 fixed point
Complex Number Support: Yes
Rst — Reset port
scalar
The optional reset port, Rst, accepts scalar
values, which can be any builtin Simulink^{®} data type including boolean
. The rate
of the input to the Rst port must be the same or
slower than that of the input data signal. The sample time of the input
to the Rst port must be a positive integer multiple
of the input sample time.
This port is unnamed until you select a
nonNone
value for the Reset
port
parameter.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
Output
Port_1 — Output signal
vector  matrix
Cumulative sum of input, specified as a vector or a matrix.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 fixed point
Parameters
Main
Sum input along — Dimension to sum along
Channels (running
sum)
(default)  Columns
 Rows
Specify the dimension along which to compute the cumulative
summations. You can choose to sum along Channels (running
sum)
, Columns
, or
Rows
. For more information, see these
sections:
Input processing — Method to process the input
Columns as channels (frame
based)
(default)  Elements as channels (sample
based)
Specify how the block processes the input when computing the running sum along the channels of the input. You can set this parameter to one of these options:
Columns as channels (frame based)
— The block treats each column of the input as a separate channel.Elements as channels (sample based)
— The block treats each element of the input as a separate channel.
Dependencies
This parameter is available only when you set the Sum
input along
parameter to Channels
(running sum)
.
Reset port — Reset type
None
(default)  Rising edge
 Falling edge
 Either edge
 Nonzero sample
Determines the reset event that causes the block to reset the sum
along channels. The rate of the input to the Rst
port must be the same or slower than that of the
input data signal. The sample time of the input to the
Rst port must be a positive integer multiple of
the input sample time. For more information, see Resetting the Running Sum.
Dependencies
This parameter is available only when you set the Sum
input along
parameter to Channels
(running sum)
.
Data Types
Note
Floatingpoint inheritance takes precedence over the data type settings defined on this pane. When inputs are floating point, the block ignores these settings. All internal data types are floating point.
Rounding mode — Rounding mode
Floor
(default)  Ceiling
 Convergent
 Nearest
 Round
 Simplest
 Zero
Specify the rounding mode for fixedpoint operations as one of the following:
Floor
Ceiling
Convergent
Nearest
Round
Simplest
Zero
For more details, see rounding mode.
Saturate on integer overflow — Saturate for fixedpoint operation
off
(default)  on
When you select this parameter, the block saturates the result of its
fixedpoint operation. When you clear this parameter, the block wraps
the result of its fixedpoint operation. For details on
saturate
and wrap
, see overflow
mode for fixedpoint operations.
Accumulator — Data type of accumulator
Inherit: Same as first
input
(default)  fixdt([],16,0)
Accumulator specifies the data type of the output of an accumulation operation in the Cumulative Sum block. For illustrations on how to use the accumulator data type in this block, see the 'FixedPoint Conversion' section in Extended Capabilities.
Inherit: Same as input
— The block specifies the accumulator data type to be the same as the input data type.fixdt([],16,0)
— The block specifies an autosigned, binarypoint, scaled, fixedpoint data type with a word length of 16 bits and a fraction length of 0.
Alternatively, you can set the Accumulator data type by using the Data Type Assistant. Click the Show data type assistant button.
For more information, see Specify Data Types Using Data Type Assistant (Simulink).
Output — Data type of output
Inherit: Same as
accumulator
(default)  Inherit: Same as input
 fixdt([],16,0)
Output specifies the data type of the output of the Cumulative Sum block. For more information on the output data type, see the 'FixedPoint Conversion' section in Extended Capabilities.
Inherit: Same as input
— The block specifies the output data type to be the same as the input data type.Inherit: Same as accumulator
— The block specifies the output data type to be the same as the accumulator data type.fixdt([],16,0)
— The block specifies an autosigned, binarypoint, scaled, fixedpoint data type with a word length of 16 bits and a fraction length of 0.
Alternatively, you can set the Output data type by using the Data Type Assistant. Click the Show data type assistant button.
For more information on the data type assistant, see Specify Data Types Using Data Type Assistant (Simulink).
Output Minimum — Minimum value the block can output
[]
(default)  scalar
Specify the minimum value the block can output. Simulink software uses this minimum value to perform:
Simulation range checking. See Specify Signal Ranges (Simulink).
Automatic scaling of fixedpoint data types.
Output Maximum — Maximum value block can output
[]
(default)  scalar
Specify the maximum value the block can output. Simulink software uses this maximum value to perform:
Simulation range checking. See Specify Signal Ranges (Simulink).
Automatic scaling of fixedpoint data types.
Lock data type settings against changes by the fixedpoint tools — Prevent fixedpoint tools from overriding data types
off
(default)  on
Select this parameter to prevent the fixedpoint tools from overriding the data types you specify in the block dialog box.
Block Characteristics
Data Types 

Direct Feedthrough 

Multidimensional Signals 

VariableSize Signals 

ZeroCrossing Detection 

More About
Computing the Running Sum Along Channels of the Input
When you set the Sum input
along
parameter to Channels (running sum)
,
the block computes the cumulative sum of the elements in each input channel. The
running sum of the current input takes into account the running sum of all previous
inputs. In this mode, you must also specify a value for the Input
processing
parameter.
When you set the Input processing parameter to:
Columns as channels (frame based)
–– The block computes the running sum along each column of the current input.Elements as channels (sample based)
–– The block computes a running sum for each element of the input across time.
Computing the Running Sum for Each Column of the Input
When you set the Input processing parameter to
Columns as channels (frame based)
, the block treats
each input column as an independent channel. As the following figure and equation
illustrate, the output has the following characteristics:
The first row of the first output is the same as the first row of the first input.
The first row of each subsequent output is the sum of the first row of the current input (time t), and the last row of the previous output (time t  T_{f}, where T_{f} is the frame period).
The output has the same size, dimension, data type, and complexity as the input.
Given an MbyN matrix input, u, the output, y, is an MbyN matrix whose first row has elements
$${y}_{1,j}(t)={u}_{1}{,}_{j}(t)+{y}_{M,j}(t{T}_{f})$$
Computing the Running Sum for Each Element of the Input
When you set the Input processing parameter to
Elements as channels (sample based)
, the block treats
each element of the input matrix as an independent channel. As the following figure
and equation illustrate, the output has these characteristics:
The first output is the same as the first input.
Each subsequent output is the sum of the current input (time t) and the previous output (time t  T_{s}, where T_{s} is the sample period).
The output has the same size, dimension, data type, and complexity as the input.
Given an MbyN matrix input, u, the output, y, is an MbyN matrix with the elements
$${y}_{i,j}(t)={u}_{i,j}(t)+{y}_{i,j}(t{T}_{s})\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\begin{array}{c}1\le i\le M\\ 1\le j\le N\end{array}$$
Resetting the Running Sum
When you are computing the running sum, you can configure the block to reset the
running sum whenever it detects a reset event at the optional Rst
port. The rate of the input to the Rst port must be the same or
slower than that of the input data signal. The sample time of the input to the
Rst port must be a positive integer multiple of the input
sample time. The reset sample time must be a positive integer multiple of the input
sample time. The input to the Rst port can be
boolean
.
If a reset event occurs while the block is performing samplebased processing, the block initializes the current output to the values of the current input. If a reset event occurs while the block is performing framebased processing, the block initializes the first row of the current output to the values in the first row of the current input.
The Reset
port
parameter specifies the reset event, which can be one of the
following:
None
disables the Rst port.Rising edge
— Triggers a reset operation when the Rst input does one of the following:Rises from a negative value to a positive value or zero
Rises from zero to a positive value, where the rise is not a continuation of a rise from a negative value to zero
Falling edge
— Triggers a reset operation when the Rst input does one of the following:Falls from a positive value to a negative value or zero
Falls from zero to a negative value, where the fall is not a continuation of a fall from a positive value to zero
Either edge
— Triggers a reset operation when the Rst input is aRising edge
orFalling edge
Nonzero sample
— Triggers a reset operation at each sample time that the Rst input is not zero
Note
When you run simulations in the Simulink
MultiTasking
mode, reset signals have a onesample
latency. When the block detects a reset event, a onesample delay occurs at the
reset port rate before the block applies the reset. For more information on
latency and the Simulink tasking modes, see Excess Algorithmic Delay (Tasking Latency) and TimeBased Scheduling and Code Generation (Simulink Coder).
Summing Along Columns
When you set the Sum input
along
parameter to Columns
, the block
computes the cumulative sum of each column of the input. In this mode, the current
cumulative sum is independent of the cumulative sums of previous inputs.
y = cumsum(u) % Equivalent MATLAB code
The output has the same size, dimension, data type, and complexity as the input. The mth output row is the sum of the first m input rows.
Given an MbyN input, u, the output, y, is an MbyN matrix whose jth column has elements
$${y}_{i,j}={\displaystyle \sum _{k=1}^{j}{u}_{k,j}}\text{}1\le i\le M$$
The block treats lengthM unoriented vector inputs as Mby1 column vectors when summing along columns.
Summing Along Rows
When you set the Sum input
along
parameter to Rows
, the block
computes the cumulative sum of the row elements. In this mode, the current
cumulative sum is independent of the cumulative sums of previous inputs.
y = cumsum(u,2) % Equivalent MATLAB code
The output has the same size, dimension, and data type as the input. The nth output column is the sum of the first n input columns.
Given an MbyN input, u, the output, y, is an MbyN matrix whose ith row has elements
$${y}_{i,j}={\displaystyle \sum _{k=1}^{j}{u}_{i,k}}\text{}1\le j\le N$$
When you sum along rows, the block treats lengthN unoriented vector inputs as 1byN row vectors.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Generated code relies on the memcpy
or
memset
function (string.h
) under certain
conditions.
FixedPoint Conversion
Design and simulate fixedpoint systems using FixedPoint Designer™.
The following diagram shows the data types used within the Cumulative Sum block for fixedpoint signals.
You can set the accumulator and output data types in the block dialog box. See Parameters.
Version History
Introduced before R2006a
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)