diff - Differences and approximate derivatives (2024)

Differences and approximate derivatives

collapse all in page

Syntax

Y = diff(X)

Y = diff(X,n)

Y = diff(X,n,dim)

Description

Y = diff(X) calculates differences between adjacent elements of X. By default, diff operates 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.

  • If X is a p-by-m table or timetable, then Y = diff(X) returns a table or timetable of size (p-1)-by-m, whose elements are the differences between the rows of X. If X is a 1-by-m table or timetable, then the size of Y is 0-by-m. (since R2023a)

example

Y = 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)).

example

Y = diff(X,n,dim) is the nth difference calculated along the dimension specified by dim. The dim input is a positive integer scalar.

example

Examples

collapse all

Differences Between Vector Elements

Open Live Script

Create a vector, then compute the differences between the elements.

X = [1 1 2 3 5 8 13 21];Y = diff(X)
Y = 1×7 0 1 1 2 3 5 8

Note that Y has one fewer element than X.

Differences Between Matrix Rows

Create a 3-by-3 matrix, then compute the first difference between the rows.

X = [1 1 1; 5 5 5; 25 25 25];Y = diff(X)
Y = 2×3 4 4 4 20 20 20

Y is a 2-by-3 matrix.

Multiple Differences

Open Live Script

Create a vector and compute the second-order difference between the elements.

X = [0 5 15 30 50 75 105];Y = diff(X,2)
Y = 1×5 5 5 5 5 5

Differences Between Matrix Columns

Open Live Script

Create a 3-by-3 matrix, then compute the first-order difference between the columns.

X = [1 3 5;7 11 13;17 19 23];Y = diff(X,1,2)
Y = 3×2 2 2 4 2 2 4

Y is a 3-by-2 matrix.

Approximate Derivatives with diff

Open Live Script

Use the diff function to approximate partial derivatives with the syntax Y = diff(f)/h, where f is a vector of function values evaluated over some domain, X, and h is an appropriate step size.

For example, the first derivative of sin(x) with respect to x is cos(x), and the second derivative with respect to x is -sin(x). You can use diff to approximate these derivatives.

h = 0.001; % step sizeX = -pi:h:pi; % domainf = sin(X); % rangeY = diff(f)/h; % first derivativeZ = diff(Y)/h; % second derivativeplot(X(:,1:length(Y)),Y,'r',X,f,'b', X(:,1:length(Z)),Z,'k')

diff - Differences and approximate derivatives (1)

In this plot the blue line corresponds to the original function, sin. The red line corresponds to the calculated first derivative, cos, and the black line corresponds to the calculated second derivative, -sin.

Differences Between Datetime Values

Open Live Script

Create a sequence of equally-spaced datetime values, and find the time differences between them.

t1 = datetime('now');t2 = t1 + minutes(5);t = t1:minutes(1.5):t2
t = 1x4 datetime 20-Jul-2024 15:09:36 20-Jul-2024 15:11:06 20-Jul-2024 15:12:36 20-Jul-2024 15:14:06
dt = diff(t)
dt = 1x3 duration 00:01:30 00:01:30 00:01:30

diff returns a duration array.

Input Arguments

collapse all

XInput array
vector | matrix | multidimensional array | table | timetable

Input array, specified as a vector, matrix, multidimensional array, table, or timetable. X can be a numeric array, logical array, datetime array, or duration array, or a table or timetable whose variables have any of these data types.

Complex Number Support: Yes

nDifference order
positive integer scalar | []

Difference order, specified as a positive integer scalar or []. The default value of n is 1.

The behavior of diff when n is larger than the dimension being operated on depends on the syntax being used:

  • With diff(X,n), where n is larger than the first non-singleton dimension, diff reduces the size of that dimension to 1 and then continues taking differences along the next non-singleton dimension.

  • With diff(X,n,dim), where n >= size(X,dim), diff returns an empty array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

dimDimension to operate along
positive integer scalar

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional p-by-m input array, A:

  • diff(A,1,1) works on successive elements in the columns of A and returns a (p-1)-by-m difference matrix.

  • diff(A,1,2) works on successive elements in the rows of A and returns a p-by-(m-1) difference matrix.

diff - Differences and approximate derivatives (2)

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Y — Difference array
scalar | vector | matrix | multidimensional array | table | timetable

Difference array, returned as a scalar, vector, matrix, multidimensional array, table, or timetable. If X is a nonempty array, then the dimension of X acted on by diff is reduced in size by n in the output.

Extended Capabilities

The diff function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

The diff function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.

See Also

gradient | prod | cumsum | sum

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

diff - Differences and approximate derivatives (3)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

diff - Differences and approximate derivatives (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5984

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.