【 MATLAB 】mod 函数介绍

版权声明:本博客内容来自于个人学习过程中的总结,参考了互联网、数据手册、帮助文档、书本以及论文等上的内容,仅供学习交流使用,如有侵权,请联系,我会重写!转载请注明地址! https://blog.csdn.net/Reborn_Lee/article/details/83504459

mod

Remainder after division (modulo operation)

除法(模运算)之后的余数

Syntax

b = mod(a,m)

Description

b = mod(a,m) returns the remainder after division of a by m, where a is the dividend and m is the divisor. This function is often called the modulo operation, which can be expressed as b = a - m.*floor(a./m). The mod function follows the convention that mod(a,0) returns a.

b = mod(a,m)返回a除以m后的余数,其中a是被除数,m是除数。此函数通常称为模运算,可表示为 b = a - m.*floor(a./m)。 mod函数遵循mod(a,0)返回a的约定。


Remainder After Division of Scalar

% Remainder After Division of Scalar
% Compute 23 modulo 5. 

b = mod(23,5)

b = 3;


Remainder After Division of Vector

Find the remainder after division for a vector of integers and the divisor 3.

a = 1:5;
m = 3;
b = mod(a,m)
b = 1×5

     1     2     0     1     2

Remainder After Division for Positive and Negative Values

Find the remainder after division for a set of integers including both positive and negative values. Note that nonzero results are always positive if the divisor is positive.

在除法之后找到一组整数,包括正值和负值。 请注意,如果除数为正,则非零结果始终为正。

a = [-4 -1 7 9];
m = 3;
b = mod(a,m)
b = 1×4

     2     2     1     0

注:这里说明下负数如何对一个正数取模,或者做mod运算,例如求:

mod(-4,3),算法流程是这样的,先根据3对负数进行处理,即-4+3+3=2,这样就处理成了正数,然后在取余运算,mod(2,3)=2。

同理,mod(-1,3),先将-1+3 = 2,之后mod(2,3)=2.

至于为什么要加3,我们不妨认为对谁取余,例如mod(a,m),a /m = n ...b,也就是a除以m得到n余b,那么a = m * n + b,我们需要求得是b,从这个式子中我们可以看出,a加上几个m之后再除以m,是不影响b的值的,所以这种处理是可以的。


Remainder After Division for Negative Divisor

Find the remainder after division by a negative divisor for a set of integers including both positive and negative values. Note that nonzero results are always negative if the divisor is negative.

在除法之后找到一个负除数,得到一组整数,包括正值和负值。 请注意,如果除数为负,则非零结果始终为负。

a = [-4 -1 7 9];
m = -3;
b = mod(a,m)
b = 1×4

    -1    -1    -2     0

注:花里胡哨的,处理的方法和被除数a是负数时原理一样,这时,如果a是正数,则对a加几个m之后变成负数,在对m取余。

Remainder After Division for Floating-Point Values

Find the remainder after division for several angles using a modulus of 2*pi. Note that mod attempts to compensate for floating-point round-off effects to produce exact integer results when possible.

使用2 * pi的模数找到除法后的几个角度的余数。 请注意,mod会尝试补偿浮点舍入效果,以便在可能的情况下生成精确的整数结果。

theta = [0.0 3.5 5.9 6.2 9.0 4*pi];
m = 2*pi;
b = mod(theta,m)
b = 1×6

         0    3.5000    5.9000    6.2000    2.7168         0

注:除数是浮点数而已,按照MATLAB能处理的精度算就是了,让它算。



Differences Between mod and rem

The concept of remainder after division is not uniquely defined, and the two functions mod and rem each compute a different variation. The mod function produces a result that is either zero or has the same sign as the divisor. The rem function produces a result that is either zero or has the same sign as the dividend.

除法后的余数的概念不是唯一定义的,并且两个函数mod和rem各自计算不同的变化。 mod函数产生的结果为零或与除数具有相同的符号。 rem函数产生的结果为零或与被除数具有相同的符号。

Another difference is the convention when the divisor is zero. The mod function follows the convention that mod(a,0) returns a, whereas the rem function follows the convention that rem(a,0)returns NaN.

另一个区别是除数为零时的约定。 mod函数遵循mod(a,0)返回a的约定,而rem函数遵循rem(a,0)返回NaN的约定。

Both variants have their uses. For example, in signal processing, the mod function is useful in the context of periodic signals because its output is periodic (with period equal to the divisor).

两种变体都有其用途。 例如,在信号处理中,mod函数在周期信号的上下文中是有用的,因为它的输出是周期性的(周期等于除数)。

Congruence Relationships

同余关系

The mod function is useful for congruence relationships: a and b are congruent (mod m) if and only if mod(a,m) == mod(b,m). For example, 23 and 13 are congruent (mod 5).

mod函数对于同余关系很有用:当且仅当mod(a,m)== mod(b,m)时,a和b是全等的(mod m)。 例如,23和13是一致的(模5)。

猜你喜欢

转载自blog.csdn.net/Reborn_Lee/article/details/83504459
mod