Main Content

mod

Remainder after division (modulo operation)

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.

example

Examples

collapse all

Compute 23 modulo 5.

b = mod(23,5)
b = 
3

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

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

     1     2     0     1     2

Find the remainder after division for a set of integers including both positive and negative values. Note that nonzero 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

Find the remainder after division by a negative divisor for a set of integers including both positive and negative values. Note that nonzero 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

Find the remainder after division for several angles using a modulus of 2*pi. Note that mod attempts to compensate for floating-point round-off effects to produce exact integer results when 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 Arguments

collapse all

Dividend, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. a must be a real-valued array of any numerical type. 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 is a duration array and m is a numeric array, then the values in m are treated as numbers of 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.

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

Divisor, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. m must be a real-valued array of any numerical type. 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 m is a duration array and a is a numeric array, then the values in a are treated as numbers of 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.

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

More About

collapse all

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

References

[1] Knuth, Donald E. The Art of Computer Programming. Vol. 1. Addison Wesley, 1997 pp.39–40.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also