David
Farruggio

MathGenie

diff

Differences and Approximate Derivatives

Syntax and Description

diff(X) calculates differences between adjacent elements of 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 = \left[\begin{array}{cccc} X(2)-X(1) & X(3)-X(2) & ... & X(m)-X(m-1) \end{array}\right]\]
  • If X is a 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 = \left[ \begin{array}{c} X(2,:)-X(1,:) \\ X(3,:)-X(2,:) \\ \vdots \\ X(p,:)-X(p-1,:) \end{array}\right] \]
diff(X,n) calculates the nth difference by applying the diff(X) operator recursively n times. In practice, this means diff(X,2) is the same as diff(diff(X)).
diff(X,n,dim) is the nth difference calculated along the dimension specified by dim. The dim input is a positive integer scalar.

Back to functions