Computes the modulus of an array. The syntax for its use is
y = mod(x,n)
where x
is matrix, and n
is the base of the modulus. The
effect of the mod
operator is to add or subtract multiples of n
to the vector x
so that each element x_i
is between 0
and n
(strictly). Note that n
does not have to be an integer. Also,
n
can either be a scalar (same base for all elements of x
), or a
vector (different base for each element of x
).
The following examples show some uses of mod
arrays.
--> mod(18,12) ans = <double> - size: [1 1] 6.000000000000000 --> mod(6,5) ans = <double> - size: [1 1] 1.000000000000000 --> mod(2*pi,pi) ans = <double> - size: [1 1] 0.000000000000000
Here is an example of using mod
to determine if integers are even
or odd:
--> mod([1,3,5,2],2) ans = <double> - size: [1 4] Columns 1 to 2 1.000000000000000 1.000000000000000 Columns 3 to 4 1.000000000000000 0.000000000000000
Here we use the second form of mod
, with each element using a
separate base.
--> mod([9 3 2 0],[1 0 2 2]) ans = <double> - size: [1 4] Columns 1 to 2 0.000000000000000 nan Columns 3 to 4 0.000000000000000 0.000000000000000