0

I am trying to perform a modulo operation in MATLAB, and I'm not sure how to convert the input variable to the correct data type for the modulo operation to complete.

Here is what I have:

sequence = 0;
....
sequence = sequence + 1;
if (modp(sequence, 3) == 0)
....

In C-ish, I'm looking to perform if (sequence % 3 == 0).

MATLAB complains that there is no modp operation for a double, and that I must use an int. However, the documentation doesn't say which integer format I need to use (i.e., int8, int64, et cetera) and none of those integer formats work.

What am I doing wrong?

Chance
  • 988
  • 2
  • 13
  • 29
  • 1
    It complains because either you have an old version of Matlab (the online documentation you're probably looking at is always for the the latest version – currently R2013b), you don't have the Symbolic Math toolbox, or because you're trying to use the MuPAD function [`modp`](http://www.mathworks.com/help/symbolic/mupad_ref/modp.html) directly in Matlab. Do you need a positive modulo, vs. a regular `sym/mod`? Do you even need symbolic capability? If you really want to call MuPAD functions from Matlab [see my answer here](http://stackoverflow.com/a/17735563/2278029). – horchler Oct 22 '13 at 19:43
  • I do have an older version of MATLAB, and I didn't even know what a "symbolic toolbox" was. I don't spend much time in MATLAB, and didn't know the key terminology to help me sort myself out. – Chance Oct 23 '13 at 21:49

1 Answers1

2

Did you realize you are using a function of the "symbolic toolbox"? I don't see any advantage in this case thus simply use mod(a,b) from Matlab (there is also a fixed point mod(a,b) and symbolic mod(a,b), don't confuse them)

http://www.mathworks.de/de/help/matlab/ref/mod.html

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 3
    These types of questions are becoming commonplace. It would be helpful if the MathWorks made things clearer in their online docs. Many users sadly don't even know about MuPAD or Matlab's symbolic capabilities, let alone how figure out when to use one or the other. – horchler Oct 22 '13 at 19:45
  • 1
    I was going to answer to just google for it, but you are right, the [mod function](http://www.mathworks.com/help/symbolic/mupad_ref/mod.html) of the symbolic toolbox ranks higher than the [built-in](http://www.mathworks.com/help/matlab/ref/mod.html) function. I don't think it is lack of documentation, but more how discoverable it is via Google or the command line help. – Bas Swinckels Oct 22 '13 at 19:56
  • I agree with both of you: I didn't even know what a "symbolic toolbox" was. I just typed "modulo MATLAB" into Google and the 'modp' function came up, so that's what I tried to use. – Chance Oct 23 '13 at 21:46