# Difference

Compute element-to-element difference along specified dimension of input

• Library:
• 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).

## Ports

### Input

expand all

Input signal, specified as a vector, matrix, or an N-D array.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `uint8` | `uint16` | `uint32` | `fixed point`
Complex Number Support: Yes

### Output

expand all

The difference between adjacent rows, columns, or a specified dimension of the input, returned as a vector, matrix, or an N-D 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

expand all

### Main Tab

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.

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 M-by-N inputs, the output is an (M – 1)-by-N 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 \left(M-1\right)$`
• `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 M-by-(N-1) 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 \left(N-1\right)$`
• `Specified dimension` –– The behavior of the block is an extension of the row-wise 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 M-by-N-by-P-by-R 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 M-by-N-by-(P–1)-by-R 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 \left(P-1\right)$`

Specify the one-based dimension along which to compute element-to-element differences.

#### Dependencies

To enable this parameter, select `Specified dimension` for the Difference along parameter.

### Data Types Tab

Specify the rounding mode for fixed-point operations as one of the following:

• `Floor`

• `Ceiling`

• `Convergent`

• `Nearest`

• `Round`

• `Simplest`

• `Zero`

For more details, see Rounding Modes.

When you select this parameter, the block saturates the result of its fixed-point operation. When you clear this parameter, the block wraps the result of its fixed-point operation. For details on `saturate` and `wrap`, see overflow mode for fixed-point operations.

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, binary-point, scaled, fixed-point 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 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 Fixed-Point Conversion in Extended Capabilities.

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, binary-point, scaled, fixed-point 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 button .

For more information on the output data type, see Fixed-Point Conversion in Extended Capabilities.

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 fixed-point data types.

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 fixed-point data types.

Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block dialog.

Note

Floating-point 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 `double` | `fixed point` | `integer` | `single` Direct Feedthrough `no` Multidimensional Signals `no` Variable-Size Signals `yes` Zero-Crossing Detection `no`

expand all

## Extended Capabilities

### Blocks

Introduced before R2006a

## Support Get trial now