Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Differences and approximate derivatives

`Y = diff(X)`

`Y = diff(X,n)`

`Y = diff(X,n,dim)`

calculates
differences between adjacent elements of `Y`

= diff(`X`

)`X`

along
the first array dimension whose size does not equal 1:

If

`X`

is a vector of length`m`

, then`Y = diff(X)`

returns a vector of length`m-1`

. The elements of`Y`

are the differences between adjacent elements of`X`

.Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]

If

`X`

is a nonempty, nonvector p-by-m matrix, then`Y = diff(X)`

returns a matrix of size (p-1)-by-m, whose elements are the differences between the rows of`X`

.Y = [X(2,:)-X(1,:); X(3,:)-X(2,:); ... X(p,:)-X(p-1,:)]

If

`X`

is a 0-by-0 empty matrix, then`Y = diff(X)`

returns a 0-by-0 empty matrix.