Documentation

mod

Symbolic modulus after division

Description

example

mod(a,b) finds the modulus after division. To find the remainder, use rem.

If a is a polynomial expression, then mod(a,b) finds the modulus for each coefficient.

Examples

Divide Integers by Integers

Find the modulus after division in case both the dividend and divisor are integers.

Find the modulus after division for these numbers.

[mod(sym(27), 4), mod(sym(27), -4), mod(sym(-27), 4), mod(sym(-27), -4)]
ans =
[ 3, -1, 1, -3]

Divide Rationals by Integers

Find the modulus after division in case the dividend is a rational number, and divisor is an integer.

Find the modulus after division for these numbers.

[mod(sym(22/3), 5), mod(sym(1/2), 7), mod(sym(27/6), -11)]
ans =
[ 7/3, 1/2, -13/2]

Divide Polynomial Expressions by Integers

Find the modulus after division in case the dividend is a polynomial expression, and divisor is an integer. If the dividend is a polynomial expression, then mod finds the modulus for each coefficient.

Find the modulus after division for these polynomial expressions.

syms x
mod(x^3 - 2*x + 999, 10)
ans =
x^3 + 8*x + 9
mod(8*x^3 + 9*x^2 + 10*x + 11, 7)
ans =
x^3 + 2*x^2 + 3*x + 4

Divide Elements of Matrices

For vectors and matrices, mod finds the modulus after division element-wise. Nonscalar arguments must be the same size.

Find the modulus after division for the elements of these two matrices.

A = sym([27, 28; 29, 30]);
B = sym([2, 3; 4, 5]);
mod(A,B)
ans =
[ 1, 1]
[ 1, 0]

Find the modulus after division for the elements of matrix A and the value 9. Here, mod expands 9 into the 2-by-2 matrix with all elements equal to 9.

mod(A,9)
ans =
[ 0, 1]
[ 2, 3]

Input Arguments

collapse all

Dividend (numerator), specified as a number, symbolic number, variable, polynomial expression, or a vector or matrix of numbers, symbolic numbers, variables, or polynomial expressions.

Divisor (denominator), specified as a number, symbolic number, or a vector or matrix of numbers or symbolic numbers.

collapse all

Modulus

The modulus of a and b is

$\mathrm{mod}\left(a,b\right)=a-b\ast \text{floor}\left(\frac{a}{b}\right),$

where floor rounds (a/b) towards negative infinity. For example, the modulus of -8 and -3 is -2, but the modulus of -8 and 3 is 1.

If b = 0, then mod(a,0) = 0.

Tips

• Calling mod for numbers that are not symbolic objects invokes the MATLAB® mod function.

• All nonscalar arguments must be the same size. If one input arguments is nonscalar, then mod expands the scalar into a vector or matrix of the same size as the nonscalar argument, with all elements equal to the corresponding scalar.