Difference
Compute elementtoelement difference along specified dimension of input
Libraries:
DSP System Toolbox /
Math Functions /
Math Operations
Description
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).
Examples
Ports
Input
Port_1 — Input signal
vector  matrix  ND array
Input signal, specified as a vector, matrix, or an ND array.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Complex Number Support: Yes
Output
Port_1 — Output signal
vector  matrix  ND array
The difference between adjacent rows, columns, or a specified dimension of the input, returned as a vector, matrix, or an ND array.
The output is the same data type and complexity as the input, but the dimension that the difference was calculated on is one less. The length of the other dimensions is unchanged.
For more details on how the output is computed, see the Difference along parameter.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Complex Number Support: Yes
Parameters
Main Tab
Running difference — Running difference
No
(default)  Yes
Specify whether the block computes a running difference.
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 depending on the Difference along parameter.Yes
–– The block computes the running difference along the columns of the input. See Running Operation for more information.
Difference along — Difference dimension
Columns
(default)  Rows
 Specified dimension
Specify whether the block computes the difference along the columns, rows, or a specified dimension of the input.
Columns
–– The block computes differences between adjacent elements in each column of the input. Equivalent MATLAB^{®} code is given by:y = diff(u)
For MbyN inputs, the output is an (M – 1)byN matrix whose jth column has these 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)$$
Rows
–– The block computes differences between adjacent elements in each row of the input. Equivalent MATLAB code is given by:y = diff(u,[],2)
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)$$
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. Equivalent MATLAB code is given by:y = diff(u,[],d)
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 the value of the Dimension parameter 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)$$
Dimension — Onebased dimension
1
(default)  2
 3
Specify the onebased dimension along which to compute elementtoelement differences.
Dependencies
To enable this parameter, select Specified dimension
for the Difference along parameter.
Data Types Tab
Rounding mode — Method of rounding operation
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 Modes.
Saturate on integer overflow — Method of overflow action
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 — Accumulator data type
Inherit: Inherit via internal
rule
(default)  Inherit: Same as input
 fixdt([],16,0)
Accumulator specifies the data type of the output of an accumulation operation in the Difference block.
Inherit: Inherit via internal rule
— The block inherits the accumulator data type based on an internal rule. For more information on this rule, see Inherit via Internal Rule.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. To use the 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).
For illustrations on how to use the accumulator data type in this block, see FixedPoint Conversion in Extended Capabilities.
Output — Output data type
Inherit: Same as accumulator
(default)  Inherit: Same as input
 fixdt([],16,0)
Output specifies the data type of the output of the Difference block.
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. To use the assistant, click the Show data type assistant button.
For more information, see Control Data Types of Signals (Simulink).
For more information on the output data type, see FixedPoint Conversion in Extended Capabilities.
Output Minimum — Minimum value that block can output
[]
(default)  scalar
Specify the minimum value the block can output. Simulink uses this minimum value to perform:
Simulation range checking. See Specify Signal Ranges (Simulink).
Automatic scaling of fixedpoint data types.
Output Maximum — Maximum value that block can output
[]
(default)  scalar
Specify the maximum value the block can output. Simulink 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.
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.
Block Characteristics
Data Types 

Direct Feedthrough 

Multidimensional Signals 

VariableSize Signals 

ZeroCrossing Detection 

More About
Running Operation
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 the Running difference 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})$$
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
FixedPoint Conversion
Design and simulate fixedpoint systems using FixedPoint Designer™.
This 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 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)