| MATLAB Function Reference | Search  Help Desk |
| mod | Examples See Also |
Modulus (signed remainder after division)
M = mod(X,Y)
mod(x,y) is x mod y.
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.
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)+yThe
mod function is useful for congruence relationships:mod(x,m) == mod(y,m).
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
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.
rem Remainder after division