diff - Differences and approximate derivatives - MATLAB (original) (raw)
Differences and approximate derivatives
Syntax
Description
[Y](#btwmxq8-1-Y) = diff([X](#btwmxq8-1-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 lengthm
, thenY = diff(X)
returns a vector of lengthm-1
. The elements ofY
are the differences between adjacent elements ofX
.
Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)] - If
X
is a nonempty, nonvector p-by-m matrix, thenY = diff(X)
returns a 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,:)] - If
X
is a 0-by-0 empty matrix, thenY = diff(X)
returns a 0-by-0 empty matrix. - If
X
is a p-by-m table or timetable, thenY = diff(X)
returns a table or timetable of size (p-1)-by-m, whose elements are the differences between the rows ofX
. IfX
is a 1-by-m table or timetable, then the size ofY
is 0-by-m. (since R2023a)
[Y](#btwmxq8-1-Y) = diff([X](#btwmxq8-1-X),[n](#btwmxq8-1-n))
calculates the nth difference by applying the diff(X)
operator recursively n
times along the first array dimension whose size does not equal 1.
[Y](#btwmxq8-1-Y) = diff([X](#btwmxq8-1-X),[n](#btwmxq8-1-n),[dim](#btwmxq8-1-dim))
is the nth difference calculated along the dimension specified bydim
. The dim
input is a positive integer scalar.
Examples
Create a vector, then compute the differences between the elements.
X = [1 1 2 3 5 8 13 21]; Y = diff(X)
Note that Y
has one fewer element than X
.
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
is 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)
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
is a 3-by-2 matrix.
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 size X = -pi:h:pi; % domain f = sin(X); % range Y = diff(f)/h; % first derivative Z = diff(Y)/h; % second derivative plot(X(:,1:length(Y)),Y,'r',X,f,'b', X(:,1:length(Z)),Z,'k')
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 = 1×4 datetime 01-Feb-2025 09:02:29 01-Feb-2025 09:03:59 01-Feb-2025 09:05:29 01-Feb-2025 09:06:59
dt = 1×3 duration 00:01:30 00:01:30 00:01:30
diff
returns a duration
array.
Input Arguments
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
Difference order, specified as a positive integer scalar or[]
. The default value of n
is1
. When n
is larger than the dimension being operated on, diff
returns an empty array.
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 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 ofA
and returns a (p-1)-by-m difference matrix.diff(A,1,2)
works on successive elements in the rows ofA
and returns a p-by-(m-1) difference matrix.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
Difference array, returned as a scalar, vector, matrix, multidimensional array, table, or timetable. If the dimension of X acted on by diff
has size greater thann, then this dimension is reduced byn
in the output. If this dimension is less thann
, it is reduced to zero and the output is empty.
Extended Capabilities
This function supports tall arrays with the limitations:
You must use the three-input syntax Y = diff(X,N,dim)
.
For more information, see Tall Arrays.
Usage notes and limitations:
- If used, the
dim
argument must be a constant at code generation time. - For input argument
X
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- Code generation does not support sparse matrix inputs for this function.
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
The two-input syntax diff(X,n)
of the diff
function now operates along only the first dimension whose size is greater than 1 for all values of n
. If n
is larger than that dimension, diff
returns an empty array. Previously, whenn
was larger than the first dimension whose size is greater than 1, diff(X,n)
reduced the size of that dimension to 1 and then calculated differences along the next dimension whose size is greater than 1.
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.
The diff
function shows improved performance when operating on vectors with at least 105 elements or when operating along the first or second dimension of matrices and multidimensional arrays with at least 5 x 105 elements.
For example, this code constructs a double with 2.5 x 107 elements and calculates differences between adjacent elements. The code is approximately 2.4x faster than in the previous release:
function timingDiff rng default N = 5000; A = rand(N);
tic for k = 1:40 D = diff(A); end toc end
The approximate execution times are:
R2021b: 2.43 s
R2022a: 1.00 s
The code was timed on a Windows® 10, Intel® Xeon® CPU E5-1650 v4 @ 3.60 GHz test system by calling thetimingDiff
function.