2. 矩阵的运算
2.1 矩阵运算包括加、减、乘、除(左除\、右除/)、逆、乘幂等。
>> A=[3,1;2,4];B=[0,3;5,6];
>> A+B,A-B,A*B,A/B,A\B,inv(A)
ans =
3 4
7 10
ans =
3 -2
-3 -2
ans =
5 15
20 30
ans =
-0.8667 0.6000
0.5333 0.4000
ans =
-0.5000 0.6000
1.5000 1.2000
ans =
0.4000 -0.1000
-0.2000 0.3000
2.2 矩阵的多项式运算
>> 2*A+3*B+5*A*B-A^2
ans =
20 79
105 158
注:数字与矩阵、矩阵与矩阵之间的*不能省略!
2.3 矩阵的关系运算与逻辑运算
Matlab中的关系运算符有:<、<=、>、>=、==、~=。关系运算的结果是逻辑值1(ture)或0(false)。
常用的逻辑运算符有&、|、~、xor(异或)。
>> C=(A<B)&(A==B)
C =
0 0
0 0
>> help &
& Logical and.
A & B performs a logical and of arrays A and B and returns an array
containing elements set to either logical 1 (TRUE) or logical 0
(FALSE). An element of the output array is set to 1 if both input
arrays contain a non-zero element at that same array location.
Otherwise, that element is set to 0. A and B must have the same
dimensions unless one is a scalar.