EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
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.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW