Written and maintained by CASRAI Editorial Board
Last updated
Short answer: in MATLAB, mod(a,m) returns the remainder after division of a by m, where a is the dividend and m is the divisor. It is a function, not an operator, and it is equivalent to a - m.*floor(a./m). For example, mod(5,2) returns 1. This guide is for researchers and analysts who need the exact rules for negative numbers, zero divisors and floating-point values, and the difference from rem. Facts are taken from the MathWorks reference pages for mod and rem (documentation release R2022a, read 2026-11-07).
What mod is used for in research
Modulo arithmetic turns up wherever a quantity repeats with a fixed period: cycling through a fixed set of indices, splitting samples into repeating groups, testing whether an integer is even or odd, and wrapping angles into a fixed interval. The MathWorks page notes that the output of mod is periodic, with a period equal to the divisor, and that a and b are congruent modulo m when mod(a,m) equals mod(b,m).
Install or access
mod is a built-in MATLAB function, so no toolbox is needed to call it. Installation and licence questions are covered in MATLAB installation and MATLAB licensing. The canonical reference is mod in the MATLAB documentation.
What does the “mod” operator do in MATLAB?
MATLAB has no modulo operator symbol. Modulo is the function mod, and writing mod(a,m) is the only form. The variants people search for (mod matlab, mod in matlab, mod function in matlab, matlab mod function) all refer to this same call. The percent sign is not modulo in MATLAB: % starts a comment.
Syntax
b = mod(a,m)
Here a is the dividend and m is the divisor. The result b is zero or has the same sign as the divisor. The documentation states the equivalent expression b = a - m.*floor(a./m).
What does 5 mod 2 mean?
It means the remainder when 5 is divided by 2, which is 1. Using the formula: 5 – 2*floor(5/2) = 5 – 2*2 = 1. In MATLAB, type mod(5,2).
Step by step: using mod
- Open MATLAB and the Command Window.
- Call the function with a dividend and a divisor:
mod(23,5). The documentation example returns 3. - Pass an array as the dividend to apply it to every element:
mod(1:5,3)returns1 2 0 1 2. - Use a negative divisor or dividend only after reading the sign rules below.
Output and how to read it
Negative numbers: mod versus rem
The two functions differ only in sign handling. mod takes the sign of the divisor; rem takes the sign of the dividend, and is defined as r = a - b.*fix(a./b). Documentation examples:
| Call | Result | Rule |
|---|---|---|
mod([-4 -1 7 9],3) |
2 2 1 0 |
Sign follows the divisor (positive) |
mod([-4 -1 7 9],-3) |
-1 -1 -2 0 |
Sign follows the divisor (negative) |
rem([-4 -1 7 9],3) |
-1 -1 1 0 |
Sign follows the dividend |
mod(-4,3) versus rem(-4,3) |
2 versus -1 |
Same inputs, different sign convention |
mod(1:5,3) and rem(1:5,3) |
1 2 0 1 2 |
Identical for non-negative values |
Zero divisor
mod(a,0) returns a, whereas rem(a,0) returns NaN. Check for a zero divisor if your code relies on either result.
Wrapping angles
Dividing by 2*pi wraps angles into the range from 0 up to (but not including) 2π. The documentation example mod([0.0 3.5 5.9 6.2 9.0 4*pi],2*pi) returns 0 3.5000 5.9000 6.2000 2.7168 0.
Floating-point values
The documentation states that mod attempts to compensate for floating-point round-off effects. Even so, for non-integer inputs compare results with a tolerance rather than testing exact equality.
Troubleshooting
- Negative result when you expected a positive one, or the reverse: you may need the other function. Use
modwhen the sign should follow the divisor,remwhen it should follow the dividend. - Unexpected value with a zero divisor:
mod(a,0)returnsaand does not raise an error. - Results that look slightly off with decimals: see the floating-point note above; the dividend or divisor may not be exactly the decimal value you typed.
Free alternatives
GNU Octave provides mod(x, y); its manual describes it as conceptually x - y .* floor (x ./ y), returning the correct modulus for negative values, so mod(-1,3) is 2 and rem(-1,3) is -1. The Octave manual also says that floating-point values within a few eps of an integer are rounded to that integer before computation for compatibility with MATLAB (Octave Utility Functions, read 2026-11-07).
| Software | Function | Documented behaviour |
|---|---|---|
| MATLAB | mod(a,m) |
Equals a - m.*floor(a./m); result takes divisor sign |
| GNU Octave | mod(x,y) |
Conceptually x - y .* floor (x ./ y); mod(-1,3) is 2 |
FAQ
What does the mod operator do in MATLAB?
There is no operator; mod(a,m) is a function that returns the remainder after division, with the sign of the divisor.
What does 5 mod 2 mean?
It is the remainder of 5 divided by 2, which is 1.
Is there MATLAB for free?
MATLAB is a licensed product; see MATLAB licensing for how access works. GNU Octave is free software with a compatible mod function, as described above.
What does %% do in MATLAB?
When %% is the first thing on a line in a script or function, it marks the start of a code section in the MATLAB Editor and Live Editor. The text after %% becomes the section title, and each section can be run separately from the rest of the file. A single percent sign % begins an ordinary comment, so neither symbol is a modulo operator; for modulo, call mod.
Related
For another core MATLAB operation see MATLAB matrix multiplication and linspace in MATLAB.
Sources
- MathWorks, mod (documentation release R2022a read 2026-11-07).
- MathWorks, rem (documentation release R2022a read 2026-11-07).
- GNU Octave manual, Utility Functions (read 2026-11-07).
- MathWorks, Create and Run Sections in Code.







