Matlab

mod function in Matlab

The following article explains how to calculate the remainder after a division using the MATLAB® mod() function. Next, we look at how to use this function to perform this mathematical operation, the input arguments and data types it supports and its properties. This topic includes practical examples that show the various applications of this function.

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.

R = mod (33, 5)

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

R = 3

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.

a = [8:13];
b = 3;
R = mod (a,b)

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

R = 2     0     1     2     0     1

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

a = [8; 9; 10; 11];
b = [1: 4];
R = mod (a, b)

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

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.

a = [-8 -12 3 -27 16 -55];
b = 5;
R = mod(a, b)

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

R =

     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.

a = [-11 -16 3 -27 36 -55];
b = -3;
R = mod (a, b)

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

R =

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

a= [10 21 3 -15; 42 33 82 13; 21 2 13  15; 5 3 31 21];
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:

R =

     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.

a= [-11 21 -13 17];
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.

R =

    4    -1    -1     2

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

a= [-11 21 -13 17];
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.

R =  -1     1     -1     2

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.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).