# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# Difference

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

## Library

Math Functions / Math Operations

`dspmathops`

## 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).

### Basic Operation

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.

#### Columnwise Differencing

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 M-by-N inputs, the output is an (M-1)-by-N 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 \left(M-1\right)$`

#### Rowwise Differencing

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 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)$`

#### Differencing Along Arbitrary Dimensions

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 M-by-N-by-P-by-R input array with elements u(i,j,k,l) and assume that Dimension 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)$`

### 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 M-by-N input matrix, the output is an M-by-N 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 \left(M-1\right)$`

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}\left(t\right)={u}_{1,j}\left(t\right)-{u}_{M,j}\left(t-{T}_{f}\right)$`

### Fixed-Point Data Types

The following diagram shows the data types used within the Difference block for fixed-point signals.

You can set the accumulator and output data types in the block dialog as discussed in Dialog Box .

## Examples

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.

## Dialog Box

The Main pane of the Difference block appears as follows.

Running difference

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.

 Note:   The ```Inherit from input (this choice will be removed - see release notes)``` option will be removed in a future release. See Difference Block Changes in the DSP System Toolbox™ Release Notes for more information.
Difference along

Specify whether the block computes the difference along the columns, rows, or specified dimension of the input.

Dimension

Specify the one-based dimension along which to compute element-to-element 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:   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.
Rounding mode

Select the rounding mode for fixed-point operations.

Overflow mode

Select the overflow mode for fixed-point operations.

Accumulator data type

Specify the accumulator data type. See Fixed-Point 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.

Output data type

Specify the output data type. See Fixed-Point 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.

Minimum

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)

• Automatic scaling of fixed-point data types

Maximum

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)

• Automatic scaling of fixed-point data types

Lock data type settings against changes by the fixed-point tools

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

## Supported Data Types

PortSupported Data Types

Input

• Double-precision floating point

• Single-precision floating point

• Fixed point

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

Output

• Double-precision floating point

• Single-precision floating point

• Fixed point

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

 Cumulative Sum DSP System Toolbox `diff` MATLAB