Main Content

rem

Remainder after division

Syntax

Description

example

rem(a,b) finds the remainder after division. If b <> 0, then rem(a,b) = a - fix(a/b)*b. If b = 0 or b = Inf or b = -Inf, then rem returns NaN.

The rem function does not support complex numbers: all values must be real numbers.

To find the remainder after division of polynomials, use quorem.

Examples

Divide Integers by Integers

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

Find the modulus after division for these numbers.

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

Divide Rationals by Integers

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

Find the remainder after division for these numbers.

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

Divide Elements of Matrices

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

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

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

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

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

Input Arguments

collapse all

Dividend (numerator), specified as a number, symbolic number, or a vector or matrix of numbers or symbolic numbers.

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

Tips

  • Calling rem for numbers that are not symbolic objects invokes the MATLAB® rem 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.

Version History

Introduced before R2006a

See Also

|