Compute elementtoelement difference along specified dimension of input
Math Functions / Math Operations
dspmathops
The Difference block computes the difference between adjacent
elements in rows, columns, or a specified dimension of the input array u
.
You can configure the block to compute the difference only within
the current input, or across consecutive inputs (running difference).
When you set the Running difference parameter
to No
, the block computes the difference
between adjacent elements in the specified dimension of the current
input. In this mode, the block can compute the difference along the
columns, rows, or a specified dimension of the input.
When you set the Difference along parameter
to Columns
, the block computes differences
between adjacent elements in each column of the input.
y = diff(u) % Equivalent MATLAB code
For MbyN inputs, the output is an (M1)byN matrix whose jth column has the following elements:
$${y}_{i,j}={u}_{i}{}_{+1}{,}_{j}{u}_{i,j}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}1\le i\le (M1)$$
When you set the Difference along parameter
to Rows
, the block computes differences
between adjacent elements in each row of the input.
y = diff(u,[],2) % Equivalent MATLAB code
The output is an Mby(N1) matrix whose ith row has the following elements:
$${y}_{i,j}={u}_{i,j+1}{u}_{i,j}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}1\le j\le (N1)$$
When you set the Difference along parameter
to Specified dimension
, the behavior of
the block is an extension of the rowwise differencing described earlier.
The block computes differences between adjacent elements along the
dimension you specify in the Dimension parameter.
y = diff(u,[],d) % Equivalent MATLAB code where d is the dimension
The output is an array whose length in the specified dimension
is one less than that of the input, and whose lengths in other dimensions
are unchanged. For example, consider an MbyNbyPbyR input
array with elements u(i,j,k,l) and assume that Dimension is 3
.
The output of the block is an MbyNby(P–1)byR array
with the following elements:
$${y}_{i,j,k,l}={u}_{i,j,k+1,l}{u}_{i,j,k,l}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}1\le k\le (P1)$$
When you set the Running difference parameter
to Yes
, the block computes the running
difference along the columns of the input.
For an MbyN input matrix, the output is an MbyN matrix whose jth column has the following elements:
$${y}_{i,j}={u}_{i+1,j}{u}_{i,j}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}2\le i\le (M1)$$
In running mode, the first element of the output for each column is the first input element minus the last input element of the previous frame. For the first frame, the block subtracts zero from the first input element.
$${y}_{1,j}(t)={u}_{1,j}(t){u}_{M,j}(t{T}_{f})$$
The following diagram shows the data types used within the Difference block for fixedpoint signals.
You can set the accumulator and output data types in the block dialog as discussed in Dialog Box .
For an example showing the modes of the Difference block, open ex_difference.
The following figure shows the output of the block when it is differencing along columns in nonrunning mode.
The following figure shows the output of the block when it is differencing along rows in nonrunning mode.
The following figure shows the second frame of block output when the block is computing the running difference.
The Main pane of the Difference block appears as follows.
Specify whether or not the block computes a running difference.
When you select No
, the block computes
the difference between adjacent elements in the current input. When
you select Yes
, the block computes the
running difference across consecutive inputs. In running mode, the
block always computes the difference along the columns of the input.
See Running Operation for more information.
Specify whether the block computes the difference along the columns, rows, or specified dimension of the input.
Specify the onebased dimension along which to compute elementtoelement differences.
This parameter is only visible when you select Specified
dimension
for the Difference along parameter.
The Data Types pane of the Difference block appears as follows.
Note: Floatingpoint inheritance takes precedence over the data type settings defined on this pane. When inputs are floating point, the block ignores these settings, and all internal data types are floating point. 
Select the rounding mode for fixedpoint operations.
Select the overflow mode for fixedpoint operations.
Specify the accumulator data type. See FixedPoint Data Types for illustrations depicting the use of the accumulator data type in this block. You can set this parameter to:
A rule that inherits a data type, for example, Inherit:
Inherit via internal rule
. For more information on this
rule, see Inherit via Internal Rule.
An expression that evaluates to a valid data type,
for example, fixdt([],16,0)
Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Accumulator data type parameter.
See Specify Data Types Using Data Type Assistant (Simulink) for more information.
Specify the output data type. See FixedPoint Data Types for illustrations depicting the use of the output data type in this block. You can set it to:
A rule that inherits a data type, for example, Inherit:
Same as accumulator
An expression that evaluates to a valid data type,
for example, fixdt([],16,0)
Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Output data type parameter.
See Control Signal Data Types (Simulink) for more information.
Specify the minimum value that the block should output. The
default value is []
(unspecified). Simulink^{®} software
uses this value to perform:
Simulation range checking (see Signal Ranges (Simulink))
Automatic scaling of fixedpoint data types
Specify the maximum value that the block should output. The
default value is []
(unspecified). Simulink software
uses this value to perform:
Simulation range checking (see Signal Ranges (Simulink))
Automatic scaling of fixedpoint data types
Select this parameter to prevent the fixedpoint tools from overriding the data types you specify on the block mask.
Port  Supported Data Types 

Input 

Output 

Cumulative Sum  DSP System Toolbox 
diff  MATLAB 