Updated March 6, 2023
Introduction to Matlab diff
Matlab diff function is used to find the difference between 2 adjacent elements in a series or array. In the case of matrices, it is used to find the adjacent rows or columns’ difference depending upon the dimension passed as the argument. By default, this function calculates the 1st order difference; however, we can calculate the higher-order difference if required.
Syntax of diff Function
Given below is the syntax of the diff function:
D = diff (A)
D = diff (A, o)
D = diff (A, o, dim)
Explanation:
- D = diff (A) is used to return the difference between the adjacent elements in input A.
- D = diff (A, o) is used to return the ordered difference between the adjacent elements in input A. The order is given by the argument ‘o’.
- D = diff (A, o, dim) is used to return the difference between the adjacent rows or columns of the input matrix A. The dimension (row or column) along which the diff function will work is decided by the argument ‘dim’.
- The formula that diff function uses is: D = [A(2) – A(1) A(3) – A(2) … A(n) – A(n – 1)], where ‘n’ is the number of terms in the array.
Please note that if we have ‘n’ number of elements in a vector or array, the diff function will return ‘n-1’ elements in the output.
Examples of Matlab diff
Given below are the examples of Matlab diff:
Example #1
In this example, we will use the diff function for a vector input.
Code:
A = [5 6 11 1 0 6 4 34 3 6] [Initializing a vector with 10 elements as the input]
D = diff (A)
[Passing the vector ‘A’ as the input to the diff function]
Input:
Output:
As we can see, we have obtained the difference between adjacent elements as the output of the diff function.
Example #2
In this example, we will use the diff function to calculate the 2nd order difference for a vector input. For this, we will pass ‘2’ as the second argument, which signifies that the 2nd order difference is to be calculated. Please note that the formula for the 2nd order difference is diff (diff (A)).
Code:
A = [15 2 10 4 10 3 4 3 13 12] [Initializing a vector with 10 elements as the input]
D = diff (A, 2)
[Passing the vector ‘A’ as the input to the diff function. The second argument is passed to get the 2nd order difference]
Input:
Output:
As we can see, we have obtained the 2nd order difference between adjacent elements as the output of the diff function. Please note that the number of elements in the output is 8; this is because, in the 2nd order difference, 2 iterations of difference calculation are performed.
Example #3
In this example, we will use the diff function with a matrix input. By default, the diff function will compute the difference along with the row elements. Also, please note that for a ‘3 x 3’ input matrix, the output will be a ‘2 x 3’ matrix.
Code:
A = [5 3 7; 1 10 13; 4 2 11] [Initializing a 3 x 3 matrix as the input]
D = diff(A)
[Passing the matrix ‘A’ as the input to the diff function]
Input:
Output:
As we can see, we have obtained the difference between adjacent row elements as the output of the diff function. Please note that the output is a ‘2 x 3’ matrix, as seen earlier.
Example #4
In this example, we will use the diff function to compute the difference between the adjacent columns of a matrix. Also, please note that for a ‘3 x 3’ input matrix, the output will be a ‘3 x 2’ matrix.
Code:
A = [2 5 5; 11 9 13; 5 2 7] [Initializing a 3 x 3 matrix as the input]
D = diff(A, 1, 2)
[Passing the matrix ‘A’ as the input to the diff function. The second argument signifies that we need 1st order difference, and the third argument signifies that we need difference along with the columns].
Input:
Output:
As we can see, we have obtained the difference between adjacent column elements as the output of the diff function. Please note that the output is a ‘3 x 2’ matrix, as seen earlier.
Conclusion
The diff function is used to find the difference between 2 adjacent elements of a series or an array. It can be used to find the difference between 2 adjacent rows or columns of a matrix. We can use the diff function to calculate the ordered difference also by passing the required order of difference as an argument.
Recommended Articles
This is a guide to Matlab diff. Here we discuss the introduction and the examples of Matlab diff for better understanding. You may also have a look at the following articles to learn more –