## 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.

**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)`

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

4.5 (6,944 ratings)

View Course

**Input:**

**Output:**

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]`

`Y = 4`

`R = mod (X, Y)`

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

**Input:**

**Output:**

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)`

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

**Input:**

**Output:**

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]`

`Y = 6`

`R = mod (X, Y)`

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

**Input:**

**Output:**

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)`

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

**Input:**

**Output:**

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]`

`Y = 4`

`R = mod (X, Y)`

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

**Input:**

**Output:**

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 –