How to check divisibility?

Hi, I need to check whether two numbers are divisible or not. But how can I do that in Matlab? e.g. Matlab gives 1.5 for 3/2 but I need to get 3/2 = 0 and 4/2 =2.

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 20 Jan 2013
Edited: Azzi Abdelmalek on 20 Jan 2013
a=3;
b=2;
out=~rem(a,b)*a/b

4 Comments

or :
~mod(a/b,1)
will return 1 if divisble, zero if not.
~mod(a,b)
is fine
mod and rem are the same when a and b have the same sign
Thanks ,it works!
However, I used rem(a,b) format after checking the help files and it worked. Anyway thank you very much.

Sign in to comment.

More Answers (1)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!