Understanding the mod(x,y) Command in MATLAB

  • MATLAB
  • Thread starter Alem2000
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses different ways to check if a number R is divisible by another number D, with D being equal to 3. One method involves using the fix function, while the other uses the mod function. The latter is explained as finding the remainder of a division process and is considered a useful tool in many programming languages. The conversation ends with the topic of mod commands and their usefulness.
  • #1
Alem2000
117
0
Hello all, i am trying to write a script file that will only print if [tex]R[/tex] is divisible by [tex]D[/tex] where [tex]D=3[/tex]
I wrote a loop statement with the condition being [tex]fix(R/D)=R/D[/tex] this works fine but there is another way that the book does it. The book uses [tex]mod(R,D)=0[/tex] as the condition. I tried typing in [tex]help mod[/tex] but the explanation that MATLAB gave was extreamly confusing, can anyone tell me what the command [tex]mod(x,y)[/tex] says...what does the [tex](x,y)[/tex] mean...thanks in adnvance.
 
Physics news on Phys.org
  • #2
mod is the modulus of command. A modulus is essentially the remainder of a division process. Here's a few examples:

4/2=2 R=0--2 goes evenly into 4 thus there is no remainder.

so, mod(4,2) would give a result of 0

9/4=2 R=1--4 goes into 9 twice leaving a remainder of 1

so, mod(9,4) would give a result of 1.

Mod commands are common to most (I can't think of a language that doesn't include this command though different syntax is used) and are very powerful/useful tools.

Hope this helped. Good luck.
 
  • #3
wow...thanks for the example, it hit the spot!
 

1. What exactly does the mod(x,y) command in MATLAB do?

The mod(x,y) command calculates the remainder after dividing x by y. It is also known as the modulo operation and is commonly used in mathematics and computer programming.

2. How is the mod(x,y) command different from the rem(x,y) command in MATLAB?

The mod(x,y) and rem(x,y) commands both calculate the remainder after dividing x by y. However, the main difference is that the mod(x,y) command always returns a positive remainder, while the rem(x,y) command can return a negative remainder.

3. Can the mod(x,y) command be used with non-integer values?

Yes, the mod(x,y) command can be used with non-integer values. MATLAB will automatically convert the values to their nearest integer before performing the modulo operation.

4. How does the mod(x,y) command handle division by zero?

If y is equal to zero, the mod(x,y) command will return a NaN (not a number) value. This is because division by zero is undefined in mathematics.

5. Are there any special cases where the mod(x,y) command may behave unexpectedly?

One special case is when the input x is a negative number. In this case, the mod(x,y) command will return a negative remainder if y is positive, and a positive remainder if y is negative. This may be unexpected for users who are not familiar with the modulo operation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top