diff

Returns a vector or matrix of differences.

Syntax

R = diff(x)

R = diff(x, k)

R = diff(x, k, dim)

Inputs

x
The matrix from which to compute differences.
Type: double | integer | logical | struct | cell
Dimension: scalar | vector | matrix
k
The number of differences to compute.
Type: integer
Dimension: scalar
dim
The dimension along which to compute differences.
Default: first non-singleton dimension.
Type: integer
Dimension: scalar

Outputs

R
The returned matrix of kth differences.

Examples

Basic diff example:

R = diff([1,4,1,2])
R = [ 3 -3 1 ]

kth diff example:

R = diff([1,5,2;8,3,4],1)
R = [Matrix] 1 x 3
7  -2  2

kth diff example with dim:

R = diff([1,5,2;8,3,4],1,2)
R = [Matrix] 2 x 2
 4  -3
-5   1

Comments

When k is provided, but dim is not, if k equals or exceeds the size of the relevant dimension, then the extra differences will be applied to the next non-singleton dimension.