Change language

Modulus in Matlab

|
  • mod(x, y) - the remainder of an integer division taking a sign into account;
  • rem(x, y) - the remainder of an integer division without taking a sign into account.

Modulus in MAtlab: mod(x,y)

b = mod(a,m) returns the remainder after division from a m, where a is the dividend and m the divisor. This function is often called a modulo operation, which can be described as b = a - m.*floor(a./m). mod function follows to agree that mod(a,0) returns a.

Modulus in Matlab

Modulus in Matlab - mod(x,y) examples

The remainder after division by a scalar

Calculate 23 modulo 5.

b = mod(23,5)
b = 3

The remainder after division from the vector

Find the remainder after division for a vector of integers and divisor 3.

a = 1:5;
m = 3;
b = mod(a,m)
b = 1×5

     1     2     0     1     2

Residual after division for positive and negative values

Find the remainder after division for a set of integers and include positive and include negative values. Note that non-zero results are always positive if the divisor is positive.

a = [-4 -1 7 9];
m = 3;
b = mod(a,m)
b = 1×4

     2     2     1     0

Residual after division for a negative divisor

Find the remainder after division by a negative divisor for a set of integers and include positive and include negative values. Note that non-zero results are always negative if the divisor is negative.

a = [-4 -1 7 9];
m = -3;
b = mod(a,m)
b = 1×4

    -1    -1    -2     0

Residual after division for floating point values

Find the remainder after division for multiple angles using modulo 2*pi. Note that mod attempts to compensate for the effects of floating point rounding will produce exact integer results, if possible.

theta = [0.0 3.5 5.9 6.2 9.0 4*pi];
m = 2*pi;
b = mod(theta,m)
b = 1×6

         0    3.5000    5.9000    6.2000    2.7168         0

Input parameters

a - Dividend

scalar | vector | matrix | multidimensional array

Dividend as a scalar, vector, matrix or multidimensional array. a must be an array with a valid sign of any numeric type. Input parameters a and m must either be of the same size or of a size that is compatible (e.g. a M- N matrix and m scalar or 1- N vector string). For more information, see. "Compatible array sizes for basic operations".

If a duration array and m a numeric array, then the values in m are treated as 24-hour days.

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

Data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | duration | char

m - Divisor

scalar | vector | matrix | multidimensional array

Divisor as a scalar, vector, matrix or multidimensional array. m must be an array with a valid sign of any numeric type. Input parameters a and m must either be of the same size or have sizes that are compatible (e.g. a M- N matrix and m scalar or 1- N vector string). For more information, see. "Compatible array sizes for basic operations".

If m is an array and a is a numeric array, then the values in a are treated as 24-hour days.

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

Data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | duration | char

Modulus in Matlab

More about modulus in Matlab

The difference between mod and rem

The concept of remainder after division is entirely undefined and the two functions mod and rem each calculate a different change. The mod function produces a result that is either zero or has the same sign as the divisor. The rem function produces a result which is either zero or has the same sign as the dividend.

Another difference is the convention when the divisor is zero. The mod function follows the convention that mod(a,0) returns a, whereas the rem function follows the convention that rem(a,0) returns NaN.

Both have their uses. For example, in signal processing, the mod function is useful in the context of periodic signals because its output is periodic (with period equal to the divisor).

Congruence relations

The mod function is useful for congruence relations: a and b are congruent (mod m) if and only if mod(a,m) == mod(b,m). For example, 23 and 13 are congruent (mod 5).

Modulus in Matlab: rem(x,y)

r = rem(a,b) returns the remainder after division from a b, where a is the dividend and b the divisor. This function is often called the remainder operation, which can be described as r = a - b.*fix(a./b). The rem function follows that rem(a,0) isnan.

Modulus in Matlab examples - rem(x,y)

Remainder after division by a scalar

Calculate the remainder after dividing 5 by 23.

a = 23;
b = 5;
r = rem(a,b)
r = 3

The remainder after division of a vector

Find the remainder after division for a vector of integers and a divisor of 3.

a = 1:5;
b = 3;
r = rem(a,b)
r = 1×5

     1 2 0 1 2
Modulus in Matlab

The remainder after division for positive and negative values

Find the remainder after division for a set of integers and include positive and include negative values. Note that non-zero results have the same sign as the dividend.

a = [-4 -1 7 9];
b = 3;
r = rem(a,b)
r = 1×4

    -1 -1 1 0

Remainder after division for floating point values

Find the remainder after division for several angles using divisor 2*pi. When possible, try to give exact integer results by compensating for the effects of floating point rounding.

theta = [0.0 3.5 5.9 6.2 9.0 4*pi];
b = 2*pi;
r = rem(theta,b)
r = 1×6

         0 3.5000 5.9000 6.2000 2.7168 0

References

[1] Donald Knuth, The Art of Programming. Addison Wesley, 1997 pp.39-40.