MATLAB Function Reference
  Go to function:
    Search    Help Desk 
diff    Examples   See Also

Differences and approximate derivatives

Syntax

Description

Y = diff(X) calculates differences between adjacent elements of X.

If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements:

If X is a matrix, then diff(X) returns a matrix of column differences:

In general, diff(X) returns the differences calculated along the first non-singleton (size(X,dim) > 1) dimension of X.

Y = diff(X,n) applies diff recursively n times, resulting in the nth difference. Thus, diff(X,2) is the same as diff(diff(X)).

Y = diff(X,n,dim) is the nth difference function calculated along the dimension specified by scalar dim. If order n equals or exceeds the length of dimension dim, diff returns an empty array.

Remarks

Since each iteration of diff reduces the length of X along dimension dim, it is possible to specify an order n sufficiently high to reduce dim to a singleton (size(X,dim) = 1) dimension. When this happens, diff continues calculating along the next nonsingleton dimension.

Examples

The quantity diff(y)./diff(x) is an approximate derivative.

Given,

diff(A) is the first-order difference along dimension 2.

diff(A,3,4) is the third-order difference along dimension 4.

See Also

gradient    Approximate gradient.

int         Integrate (see Symbolic Toolbox).

prod        Product of array elements

sum         Sum of array elements



[ Previous | Help Desk | Next ]