%% Difference Between rem and mod
% Define X and Y with different signs.

X = 5;
Y = -2;

%
% Compute the remainder after division with rem, then compute the modulus after division with mod.

R = rem(X,Y)

%

M = mod(X,Y)

% rem(X,Y) and mod(X,Y) are equal if X and Y have the same sign, but differ by Y if X and Y have different signs. Notice that rem retains the sign of X, while mod retains the sign of Y.
% Copyright 2012 The MathWorks, Inc.