Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB mod Function: Syntax, rem Difference and Examples

How MATLAB mod(a,m) works: syntax, negative-number sign rules, mod versus rem, zero divisor and angle wrapping, with documented examples.

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

  1. Open MATLAB and the Command Window.
  2. Call the function with a dividend and a divisor: mod(23,5). The documentation example returns 3.
  3. Pass an array as the dividend to apply it to every element: mod(1:5,3) returns 1 2 0 1 2.
  4. 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 mod when the sign should follow the divisor, rem when it should follow the dividend.
  • Unexpected value with a zero divisor: mod(a,0) returns a and 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB mod Function: Syntax, rem Difference and Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →