Skip to Main Content Skip to Search
Product Documentation

Differencing Data

Three MATLAB functions perform finite difference calculations.

Function

Description

del2

Discrete Laplacian of a matrix

diff

Differences between successive elements of a vector; numerical partial derivatives of a vector

gradient

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)]. Consider using 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, use diff to determine certain characteristics of vectors. For example, 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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS