10.Matlab运算
10.1 算术运算
10.1.2 普通算术运算
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
^ | 乘方 |
a = 3;
b = 1;
c = a + b;
a = 4;
b = 1;
c = a - b;
uint8_a = uint8(1);
uint8_b = uint8(5);
uint8_c = uint8_a + uint8_b;
uint16_a = uint16(50000);
% uint16_c = uint16_a + uint8_b; % error
uint16_c = uint16_a + uint16(uint8_b);
a = 2;
b = 3;
c = a * b;
a = 8;
b = 2;
c = a / b;
a = 8;
b = 3;
c = a / b;
uint8_a = uint8(8);
uint8_b = uint8(3);
uint8_c = uint8_a / uint8_b; % 执行结果为3,注意c语言执行结果是2
d = 2^4; % 乘方,结果为16。跟c语言运算不一样
10.1.2 余数和模数
求余运算用rem函数,求模运算用mod函数
uint8_d = rem(uint8_a, uint8_b); % 余数运算
uint8_e = mod(uint8_a, uint8_b); % 求模运算
rem_a = rem(8,-3); % 余数为2
mod_a = mod(8,-3); % 模数为-1
10.2 关系运算
< | 小于 |
> | 大于 |
== | 等于 |
~= | 不等于 |
<= | 小于等于 |
>= | 大于等于 |
c语言不等于是!=
bool_a = 1 == 2; % bool_a结果为false,逻辑值0
bool_b = false == bool_a; % false等于false,所以bool_a等于true,逻辑值1
bool_c = 100 > 1;
bool_d = 100 < 1;
bool_e = 100 >= 1;
bool_f = 100 <= 1;
bool_g = 100 <= 100;
bool_h = 100 < 100;
10.3 逻辑运算
& | 与 |
| | 或 |
~ | 非 |
&& | 与,标量 |
|| | 或,标量 |
c语言&表示位与,|位或
logical_a = true & true;
logical_b = true & false;
logical_c = false & false;
logical_d = true | true;
logical_e = true | false;
logical_f = false | false;
logical_g = ~true;
logical_h = ~false;
logical_i = (bool_a & bool_b) | bool_c;