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 Mod
 

Matlab Mod

Updated March 6, 2023

Matlab Mod

 

 

Introduction to Matlab Mod

MATLAB mod function is used to find the remainder when 2 numbers are divided. In the language of mathematics, the 2 numbers are called dividend (one which is divided) and divisor (one by which the dividend is divided). For example, when we use 7 and 2 as the inputs for the mod function, the output will be 1. This is because the remainder when 7 is divided by 2 is 1.

Watch our Demo Courses and Videos

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

Syntax of the mod function:

R = mod (X, Y)

Details of the mod function:

R = mod (X, Y) is used to return the remainder when the Dividend ‘X’ is divided by the Divisor ‘Y’.

Let us now understand how to use the mod function in MATLAB. We will discuss the code to do the following:

a. Use of the mod function for scalar input
b. Use of the mod function for a set of integer values
c. Use of the mod function for negative input
d. Use of the mod function for decimal input
e. Use of the mod function for vector input

Examples

Let us discuss examples of Matlab Mod.

Example #1

In this example, we will use the mod function for scalar inputs.

Code:

R = mod (15, 6)

[Passing dividend (15) and divisor (6) as inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

Matlab Mod 1

Output:

Matlab Mod 2

As we can see, we have obtained 3 as the output of the mod function which is the remainder when 15 is divided by 6.

Example #2

In this example, we will use the mod function for a set of scalar inputs. The mod function will calculate remainder when each of these scalars is divided by the divisor passed as the second argument.

Code:

X = [15 3 6 13 12]

[Declaring the set of inputs to be used as dividends]

Y = 4

[Declaring the divisor]

R = mod (X, Y)

[Passing ‘X’ and ‘Y’ as the inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

Matlab Mod 3

Output:

example 2

As we can see, we have obtained the remainder for each element passed as the input to the mod function.

Example #3

In this example, we will use the mod function for negative scalar inputs, i.e. our dividend will be a negative number.

Code:

R = mod (-15, 8)

[Passing dividend (-15) and divisor (8) as inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

Matlab Mod 5

Output:

Matlab Mod 6

As we can see, we have obtained 1 as the output of the mod function, which is the remainder when -15 is divided by 8.

Example #4

In this example, we will use the mod function for a set of scalar inputs with both positive and negative numbers. The mod function will calculate remainder when each of these scalars is divided by the divisor passed as the second argument.

Code:

X = [-12 30 -5 3 -34]

[Declaring the set of inputs to be used as dividends]

Y = 6

[Declaring the divisor]

R = mod (X, Y)

[Passing ‘X’ and ‘Y’ as the inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

Matlab Mod 7

Output:

Matlab Mod 8

As we can see, we have obtained the remainder for each element passed as the input to the mod function.

In the above examples, we have used integers as the dividends. However, we can also use the decimal number as an input to the mod function. Let us understand this with an example.

Example #5

In this example, we will use the mod function for a decimal input, i.e. our dividend will be a decimal number.

Code:

R = mod (7.34, 3)

[Passing dividend (7.34) and divisor (3) as inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

example 6

Output:

example 6-1

As we can see, we have obtained 1.34 as the output of the mod function, which is the remainder when 7.34 is divided by 3.

Example #6

In this example, we will use the mod function for a vector input, i.e. our dividend will be a vector with a range of integers.

Code:

X = [5:10]

[Declaring the vector with the range 5 to 10, to be considered as dividends]

Y = 4

[Declaring the divisor]

R = mod (X, Y)

[Passing ‘X’ and ‘Y’ as the inputs to the mod function]

This is how our input and output will look like in MATLAB:

Input:

example 6-3

Output:

example 6-4

As we can see, we have obtained the remainder for each element in the range 5 to 10, passed as a vector to the mod function.

Conclusion

  • The mod function is used to find the remainder when 2 numbers are divided.
  • It can be used for both positive and negative numbers.
  • We can use integers and vectors as input of the mod function.

Recommended Articles

This is a guide to Matlab Mod. Here we discuss introduction, syntax, and many date functions available in DB2 RDBMS. You may also have a look at the following articles to learn more –

  1. Matlab limit
  2. Plot graph Matlab
  3. Nyquist Matlab
  4. Matlab Saveas

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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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?

🚀 Limited Time Offer! - 🎁 ENROLL NOW