【发布时间】:2015-08-11 08:28:10
【问题描述】:
所以我需要将矩阵右手除法从 Matlab 重写为 C++:
At = (xPow*yPow')/(yPow*yPow');
我模拟了一些矩阵:
>> xPow*yPow'
ans =
0.0004 0.0040 0.0004 0.0004
0.0014 0.0263 0.0014 0.0014
0.0004 0.0012 0.0004 0.0004
0.0012 0.0053 0.0012 0.0012
和
>> yPow*yPow'
ans =
0.0001 0.0004 0.0001 0.0001
0.0004 0.0256 0.0004 0.0004
0.0001 0.0004 0.0001 0.0001
0.0001 0.0004 0.0001 0.0001
Matlab 为 (xPow*yPow')/(yPow*yPow') 和 xPow*yPow' * inv(yPow*yPow') 返回相同的结果。
>> xPow*yPow' * inv(yPow*yPow')
ans =
36.1259 0.1127 -30.3163 -2.6999
40.6472 0.8810 -19.7529 -11.8430
-1.5578 -0.0182 12.1397 -7.0087
124.4466 0.0594 -130.0163 16.6710
>> At = (xPow*yPow')/(yPow*yPow')
At =
36.1259 0.1127 -30.3163 -2.6999
40.6472 0.8810 -19.7529 -11.8430
-1.5578 -0.0182 12.1397 -7.0087
124.4466 0.0594 -130.0163 16.6710
Eigen 库有函数 .inverse() 所以我想我可以用它来实现这些矩阵的除法:
xyPowMult(0,0) = 0.0004;
xyPowMult(0,1) = 0.0040;
xyPowMult(0,2) = 0.0004;
xyPowMult(0,3) = 0.0004;
xyPowMult(1,0) = 0.0014;
xyPowMult(1,1) = 0.0263;
xyPowMult(1,2) = 0.0014;
xyPowMult(1,3) = 0.0014;
xyPowMult(2,0) = 0.0004;
xyPowMult(2,1) = 0.0012;
xyPowMult(2,2) = 0.0004;
xyPowMult(2,3) = 0.0004;
xyPowMult(3,0) = 0.0012;
xyPowMult(3,1) = 0.0053;
xyPowMult(3,2) = 0.0012;
xyPowMult(3,3) = 0.0012;
yyPowMult(0,0) = 0.0001;
yyPowMult(0,1) = 0.0004;
yyPowMult(0,2) = 0.0001;
yyPowMult(0,3) = 0.0001;
yyPowMult(1,0) = 0.0004;
yyPowMult(1,1) = 0.0256;
yyPowMult(1,2) = 0.0004;
yyPowMult(1,3) = 0.0004;
yyPowMult(2,0) = 0.0001;
yyPowMult(2,1) = 0.0004;
yyPowMult(2,2) = 0.0001;
yyPowMult(2,3) = 0.0001;
yyPowMult(3,0) = 0.0001;
yyPowMult(3,1) = 0.0004;
yyPowMult(3,2) = 0.0001;
yyPowMult(3,3) = 0.0001;
AtTemp = xyPowMult * yyPowMult.inverse();
cout << " x*y' " << endl;
cout << xyPowMult << endl << endl;
cout << " y*y' " << endl;
cout << yyPowMult << endl << endl;
cout << " x*y' * inv(y*y') " << endl;
cout << xyPowMult * yyPowMult.inverse() << endl << endl;
控制台结果显示不同的结果:
x*y'
0.0004 0.004 0.0004 0.0004
0.0014 0.0263 0.0014 0.0014
0.0004 0.0012 0.0004 0.0004
0.0012 0.0053 0.0012 0.0012
y*y'
0.0001 0.0004 0.0001 0.0001
0.0004 0.0256 0.0004 0.0004
0.0001 0.0004 0.0001 0.0001
0.0001 0.0004 0.0001 0.0001
x*y' * inv(y*y')
-1.#IND -1.#IND -1.#IND -1.#IND
-1.#IND -1.#IND -1.#IND -1.#IND
-1.#IND -1.#IND -1.#IND -1.#IND
-1.#IND -1.#IND -1.#IND -1.#IND
所以我的问题是:
- 为什么结果不同?
- 如何使用 Eigen 实现矩阵“斜线”除法?
- 如果不是 Eigen,您能否提出任何简单的方法?
【问题讨论】: