| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
Three MATLAB functions perform finite difference calculations.
Function | Description |
|---|---|
Discrete Laplacian of a matrix | |
Differences between successive elements of a vector; numerical partial derivatives of a vector | |
Numerical partial derivatives of a matrix |
The diff function computes the difference between successive elements in a numeric vector. That is, diff(X) is [X(2)-X(1) X(3)-X(2)...X(n)-X(n-1)]. You might want to perform this operation on your data if you are more interested in analyzing the changes in the values, rather than the absolute values.
For a vector A,
A = [9 -2 3 0 1 5 4]; diff(A) ans = -11 5 -3 1 4 -1
Besides computing the first difference, you can use diff to determine certain characteristics of vectors. For example, you can use diff to determine whether the vector values are monotonically increasing or decreasing, or whether a vector has equally spaced elements.
The following table provides examples for using diff with a vector x.
Test | Description |
|---|---|
any(diff(x)==0) | Tests whether there are any repeated elements in X |
all(diff(x)>0) | Tests whether the values are monotonically increasing |
all(diff(diff(x))==0) | Tests for equally spaced vector elements |
![]() | Detrending Data | Descriptive Statistics | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |