matlab里的'/'不完全等于矩阵除法。
你可以用help mrdivide看一下'/'的帮助:
>> help mrdivide
/ Slash or right matrix divide.
A/B is the matrix division of B into A, which is roughly the
same as A*INV(B) , except it is computed in a different way.
More precisely, A/B = (B'\A')'. See MLDIVIDE for details.
就是说A/B可以大致看成A*inv(B),但用的是另一种方法。更确切的讲A/B = (B'\A')'。
那再看看'\'(左除或者反除)是什么东东。
>> help mldivide
\ Backslash or left matrix divide.
A\B is the matrix division of A into B, which is roughly the
same as INV(A)*B , except it is computed in a different way.
If A is an N-by-N matrix and B is a column vector with N
components, or a matrix with several such columns, then
X = A\B is the solution to the equation A*X = B computed by
Gaussian elimination. A warning message is printed if A is
badly scaled or nearly singular. A\EYE(SIZE(A)) produces the
inverse of A.
If A is an M-by-N matrix with M < or > N and B is a column
vector with M components, or a matrix with several such columns,
then X = A\B is the solution in the least squares sense to the
under- or overdetermined system of equations A*X = B. The
effective rank, K, of A is determined from the QR decomposition
with pivoting. A solution X is computed which has at most K
nonzero components per column. If K < N this will usually not
be the same solution as PINV(A)*B. A\EYE(SIZE(A)) produces a
generalized inverse of A.
就是说当A是N阶方阵B为N行的列向量时,X=A\B就是线性方程组A*X=B的解,算法是用高斯消去法。A\EYE(SIZE(A))产生的是方阵A的逆矩阵。
如果A是M*N的矩阵且M≠N,B是跟A行数(M行)相同的列向量时,X=A\B是非满秩的线性方程组A*X=B的解系,A的秩K由QR分解得出。如果K
总而言之,A\B就是求A*X=B的解,你可以看作是A的逆矩阵,只不过是广义逆矩阵,这样A不是方阵也可以计算的。
至于A/B,在解线性方程组上比\少用一些,因为通常都把B写成列向量,所以用反除\就可以了。用/的话,B通常是行向量。
可以把B/A看作是X*A=B的解,这里B的列数等于A的列数。
A\B=pinv(A)*B
A/B=A*pinv(B)
举个例子:
>> A=pascal(3) %A赋值为3*3的方阵。
A =
1 1 1
1 2 3
1 3 6
>> b=[1:3]' % b是3*1的列向量。
b =
1
2
3
>> x=A\b % 用反除求Ax=b的解,结果x是个列向量,注意是A\b不是b\A
x =
0
1
0
>> A*x % 验证一下A*x刚好等于b
ans =
1
2
3
>> x=b'/A' % 这回是正除了,不过b'是行向量,A'也倒一下,正除的时候就是b'/A'了,不是A'/b'了,结果x是个行向量
x =
0 1 0
>> x*A' % 验证一下,跟b'一样。
ans =
1 2 3
>> A=rand(3,4) % 这回重新赋值,A不是方阵了,是3*4的矩阵
A =
0.5298 0.3798 0.4611 0.0592
0.6405 0.7833 0.5678 0.6029
0.2091 0.6808 0.7942 0.0503
>> x=A\b % 实际上方程组没有唯一确定的解,而是无数解,所以解出来的是一个特解
x =
-1.5132
4.9856
0
-1.5528
>> A*x % 验证,跟b相等。
ans =
1.0000
2.0000
3.0000
>> A=rand(3,2) % 再看看3*2的矩阵,行数>列数的情况
A =
0.4154 0.0150
0.3050 0.7680
0.8744 0.9708
>> x=A\b
x =
1.8603
1.5902
>> A*x % 验证一下,嗯?怎么不等于b了?
ans =
0.7966
1.7886
3.1704
% 为什么呢?因为方程数(行数)太多,未知数(列数)个数太少,2个未知数,用2个线性无关的方程就可以求确定的解了,现在方程多了,不能同时满足所有方程,所以实际上是无解,只不过matlab里用的是一个最小二乘意义上的近似解,所以验证时不等,只是尽可能近似的满足所有方程。
这是 X*A=B 方程组
a./b是数组相除
>> a=[1;2;3]
a =
1
2
3
>> b=[1;2;3]
b =
1
2
3
>> a/b
ans =
0 0 0.3333
0 0 0.6667
0 0 1.0000
>> a./b
ans =
1
1
1