There are two problems here.
Table of Contents
- Description
- Differences Between Vector Elements
- Differences Between Matrix Rows
- Multiple Differences
- Differences Between Matrix Columns
- Approximate Derivatives with diff
- Differences Between Datetime Values
- Input Arguments
- X — Input array vector | matrix | multidimensional array
- n — Difference order positive integer scalar | []
- dim — Dimension to operate along positive integer scalar
- Output Arguments
- Y — Difference array scalar | vector | matrix | multidimensional array
- Extended Capabilities
- Tall Arrays Calculate with arrays that have more rows than fit in memory.
- C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.
- Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.
- GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
- Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
- Version History
- R2022a: Improved performance with large number of elements
First, you swapped the order of arguments in np.diff. MATLAB and Python use the same argument order. Python supports named arguments, so it is often better to use the argument name to avoid this sort of problem.
Second, python indexing starts with 0, while MATLAB indexing starts with 1. This applies to axes as well, so MATLAB's axis 2 is Python's axis 1.
So the correct function call in Python is np.diff(fimg, 1, 1), but np.diff(fimg, axis=1) is better IMO.
MATLAB:
>> a = reshape(1:100, 10, [])' a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 >> diff(a,1, 2) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1Python:
>>> a = np.arange(100).reshape(10, -1) >>> print(a) [[ 0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19] [20 21 22 23 24 25 26 27 28 29] [30 31 32 33 34 35 36 37 38 39] [40 41 42 43 44 45 46 47 48 49] [50 51 52 53 54 55 56 57 58 59] [60 61 62 63 64 65 66 67 68 69] [70 71 72 73 74 75 76 77 78 79] [80 81 82 83 84 85 86 87 88 89] [90 91 92 93 94 95 96 97 98 99]] >>> print(np.diff(a, axis=1)) [[1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1]]Main Content
Differences and approximate derivatives
Syntax
Description
example
Y = diff(X) calculates differences between adjacent elements of X 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.
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.
Examples
collapse all
Differences Between Vector Elements
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.
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 is a 2-by-3 matrix.
Multiple Differences
Create a vector and compute the second-order difference between the elements.
X = [0 5 15 30 50 75 105]; Y = diff(X,2)Differences Between Matrix Columns
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.
Approximate Derivatives with diff
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.
Differences Between Datetime Values
Create a sequence of equally-spaced datetime values, and find the time differences between them.
diff returns a duration array.
Input Arguments
collapse all
X — Input array vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array. X can be a numeric array, logical array, datetime array, or duration array.
Complex Number Support: Yes
n — Difference order positive integer scalar | []
Difference order, specified as a positive integer scalar or []. The default value of n is 1.
It is possible to specify n sufficiently large so that dim reduces to a single (size(X,dim) = 1) dimension. When this happens, diff continues 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
dim — Dimension 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 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 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.
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Output Arguments
collapse all
Y — Difference array scalar | vector | matrix | multidimensional array
Difference array, returned as a scalar, vector, matrix, or multidimensional array. 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
Tall Arrays Calculate with arrays that have more rows than fit in memory.
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.
C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If supplied, the arguments representing the number of times to apply diff and the dimension along which to calculate the difference must be constants.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
expand all
R2022a: Improved performance with large number of elements
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. It runs 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 endThe 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 the timingDiff function.
- Trial Software
- Trial Software
- Product Updates
- Product Updates