EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab diff
 

Matlab diff

Updated March 6, 2023

Matlab diff

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Matlab diff 1

Output:

Matlab diff 2

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:

calculate the 2nd order difference for a vector input

Output:

Matlab diff 4

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:

compute the difference along the row elements

Output:

Matlab diff 6

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:

between the adjacent columns of a matrix

Output:

Matlab diff 8

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 –

  1. What is Matlab?
  2. Matlab limit
  3. Plot graph Matlab
  4. Absolute Value Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW