mod

Syntax

Description

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

Input Arguments

a — Dividendscalar | vector | matrix | multidimensional array

Dividend, specified as a scalar, vector, matrix, or multidimensional
array. a must be a real-valued array of any numerical
type. Numeric inputs a and m must
either be the same size or have sizes that are compatible (for example, a is
an M-by-N matrix and m is
a scalar or 1-by-N row vector).
For more information, see Compatible Array Sizes for Basic Operations.

If a and m are duration
arrays, then they must be the same size unless one is a scalar. If
one input is a duration array, the other input can be a duration array
or a numeric array. In this context, mod treats
numeric values as a number of standard 24-hour days.

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

m — Divisorscalar | vector | matrix | multidimensional array

Divisor, specified as a scalar, vector, matrix, or multidimensional
array. m must be a real-valued array of any numerical
type. Numeric inputs a and m must
either be the same size or have sizes that are compatible (for example, a is
an M-by-N matrix and m is
a scalar or 1-by-N row vector).
For more information, see Compatible Array Sizes for Basic Operations.

If a and m are duration
arrays, then they must be the same size unless one is a scalar. If
one input is a duration array, the other input can be a duration array
or a numeric array. In this context, mod treats
numeric values as a number of standard 24-hour days.

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

More About

Differences Between mod and rem

The concept of remainder after division is
not uniquely defined, and the two functions mod and rem each
compute a different variation. The mod function
produces a result that is either zero or has the same sign as the
divisor. The rem function produces a result that
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 variants 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 Relationships

The mod function is useful
for congruence relationships: 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).