## MATLAB mod() function syntax

R = mod(a, b)

**Expression**

R = a – b.*floor(a./b)

## MATLAB mod() function description and examples

The function mod() returns in “R” the remainder of the division of the dividend “a” by the divisor “b”. The mod function is similar to the rem function, with the only difference being that it returns a result that is zero or has the same sign as the divisor, while the rem function returns a result that is zero or has the same sign as the dividend.

The input argument types for divisor and dividend can be vector, matrix, scalar, or multidimensional array, and the supported data types are single, double, char, logical, duration, int8, int16, int32, int64, uint8, uint16, uint32, or uint64.

The mod function has the following input rules that must be followed:

Dividends specified as a vector, scalar, matrix, or multidimensional arrays must contain real values.

If one input has an integer data type, the other input must have the same integer data type or be a scalar double.

The input arguments should be the same size or compatible array sizes for basic operations. For example:

In cases where input is escalar

When the inputs are a matrix and a column vector.

One column vector and the other is a row vector

These rules apply to two-dimensional arrays. For more information, see the article Compatible array sizes for basic operations.

In cases where these rules are not met, MATLAB® displays the following error message:

“Arrays have incompatible sizes for this operation.”

Using these basic rules, we will now see how to obtain the remainder after division with the mod function in MATLAB.

## How to get the remainder after dividing a scalar by another scalar.

In the following example, we will see how to get the remainder in “R” after dividing a scalar number of 33 by 5.

As a result of this operation, the mod function returns:

## How to get the remainder after division of a row vector by a scalar.

In this example, we will see how to obtain the remainder after a scalar’s division in a row vector.

b = 3;

R = mod (a,b)

As a result of this operation, the mod function returns:

## Remainder of the division of a column vector by a row vector

As we saw earlier in the basic operations rules, when a column vector is operated on a row vector, the result is a matrix of n columns a by n rows “b”.

b = [1: 4];

R = mod (a, b)

In this case, the mod function returns the following array in R.

0 0 2 0

0 1 0 1

0 0 1 2

0 1 2 3

## How to obtain the remainder after a division of a row vector of elements with values of a positive and negative sign by a scalar of positive sign

The following example shows how to obtain the remainder after a division of a row vector “a” of elements with values of a positive and negative sign by a scalar “b” of a positive sign.

b = 5;

R = mod(a, b)

As a result of this operation, the mod function returns:

2 3 3 3 1 0

Note: in cases where the results are less than zero, the mod function will return results with a positive sign as long as the divisor has a positive sign.

## How to get the remainder after dividing a row vector with positive and negative sign elements by a negative sign scalar divisor.

In this example, we will see how to obtain the remainder after dividing a row vector with elements of positive and negative signs by a scalar divisor of negative signs.

b = -3;

R = mod (a, b)

As a result of this operation, the mod function returns:

-2 -1 0 0 0 -1

In this case, since the divisor has a negative sign, all the not zero results also have a negative sign.

## How to get the remainder after a division of a square matrix.

In this example, we will see how to obtain the remainder after dividing a square matrix.

b= [1 2 3 -5; 4 3 2 1; 2 3 4 5; 5 3 2 1];

R = mod(a, b)

As a result of this operation, the mod function returns:

0 1 0 0

2 0 0 0

1 2 1 0

0 0 1 0

## Differences between mod and rem functions.

In the following examples, we will see the differences between the mod and rem functions to calculate the remainder after a division in MATLAB. Let’s see the following calculation.

b= [5 -2 -3 5];

R= mod(a, b)

When this operation is performed with the rem() function, values with a negative sign will take on the same sign as the divisor.

4 -1 -1 2

Now let’s see what happens when we perform this operation with the rem() function.

b= [5 -2 -3 5];

R= rem (a, b)

When this operation is performed with the rem() function, values with a negative sign will take on the same sign as the dividend.

## Conclusion:

This article has explained how to use MATLAB’s basic function to solve the remainder after division operations and included some practical examples using different arrays and data types. The input arguments and accepted data type were also detailed.

We hope this MATLAB article has been helpful to you. Check out other Linux Hint articles for more tips and information.