| MATLAB Function Reference | ![]() |
Modulus (signed remainder after division)
Syntax
M = mod(X,Y)
Definition
Description
M = mod(X,Y)
returns the remainder X - Y.*floor(X./Y) for nonzero Y, and returns X otherwise. mod(X,Y) always differs from X by a multiple of Y.
Remarks
So long as operands X and Y are of the same sign, the function mod(X,Y) returns the same result as does rem(X,Y). However, for positive X and Y,
mod(-x,y) = rem(-x,y)+y
The mod function is useful for congruence relationships: x and y are congruent (mod m) if and only if mod(x,m) == mod(y,m).
Examples
mod(13,5)
ans =
3
mod([1:5],3)
ans =
1 2 0 1 2
mod(magic(3),3)
ans =
2 1 0
0 2 1
1 0 2
Limitations
Arguments X and Y should be integers. Due to the inexact representation of floating-point numbers on a computer, real (or complex) inputs may lead to unexpected results.
See Also
| mlock | more | ![]() |