Main Content

diff

Differences and approximate derivatives

Description

example

Y= diff(X)calculates differences between adjacent elements ofXalong the first array dimension whose size does not equal 1:

  • IfXis a vector of lengthm, thenY = diff(X)返回一个vector of lengthm-1. The elements ofYare the differences between adjacent elements ofX.

    Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]

  • IfXis a nonempty, nonvector p-by-m matrix, thenY = diff(X)返回一个matrix of size (p-1)-by-m, whose elements are the differences between the rows ofX.

    Y = [X(2,:)-X(1,:); X(3,:)-X(2,:); ... X(p,:)-X(p-1,:)]
  • IfXis a 0-by-0 empty matrix, thenY = diff(X)返回一个0-by-0 empty matrix.

example

Y= diff(X,n)calculates the nth difference by applying thediff(X)operator recursivelyntimes. In practice, this meansdiff(X,2)is the same asdiff(diff(X)).

example

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

Examples

collapse all

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

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

Note thatYhas one fewer element thanX.

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×34 4 4 20 20 20

Yis a 2-by-3 matrix.

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×55 5 5 5 5

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×22 2 4 2 2 4

Yis a 3-by-2 matrix.

Use thedifffunction to approximate partial derivatives with the syntaxY = diff(f)/h, wherefis a vector of function values evaluated over some domain,X, andhis an appropriate step size.

For example, the first derivative ofsin(x)with respect toxiscos(x), and the second derivative with respect toxis-sin(x). You can usediffto 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')

Figure contains an axes object. The axes object contains 3 objects of type line.

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.

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 datetimeColumns 1 through 3 26-Feb-2022 12:12:08 26-Feb-2022 12:13:38 26-Feb-2022 12:15:08 Column 4 26-Feb-2022 12:16:38
dt = diff(t)
dt =1x3 duration00:01:30 00:01:30 00:01:30

diff返回一个durationarray.

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.Xcan be a numeric array, logical array, datetime array, or duration array.

Complex Number Support:Yes

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

It is possible to specifynsufficiently large so thatdimreduces to a single (size(X,dim) = 1) dimension. When this happens,diffcontinues calculating along the next array dimension whose size does not equal 1. This process continues until a 0-by-0 empty matrix is returned.

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

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 of size greater than 1.

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

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

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

diff(A,1,1) column-wise computation and diff(A,1,2) row-wise computation

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

Output Arguments

collapse all

Difference array, returned as a scalar, vector, matrix, or multidimensional array. IfXis a nonempty array, then the dimension ofXacted on bydiffis reduced in size bynin the output.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

|||