问题描述
我正在使用类似Matlab的程序Octave 3.8.1.我想将1/x
概括为x
可以是标量或矩阵的情况.对于大多数x
,用inv(x)
或pinv(x)
替换1/x
均适用,除了:
I am using Octave 3.8.1, a Matlab-like program. I'd like to generalize 1/x
to the case where x
may be a scalar or a matrix. Replacing 1/x
with inv(x)
or pinv(x)
works for most x
, except:
octave:1> 1/inf
ans = 0
octave:2> pinv([inf])
ans = NaN
octave:3> inv([inf])
warning: inverse: matrix singular to machine precision, rcond = 0
ans = Inf
此后,我应该将NaN转换为0吗?还是我错过了什么?谢谢!
Should I convert NaN to 0 afterwards to get this to work? Or have I missed something? Thanks!
推荐答案
Moore-Penrose伪Mat 和octave pinv
的基础是通过与inv
函数完全不同的算法实现的.更具体地说,奇异值分解,它需要有限的值的矩阵(它们也不能为sparse
).您没有说矩阵是否为正方形. pinv
的真正用途是解决非正方形系统( over- 或未确定).
The Moore–Penrose pseudo inverse, which is the basis for Matab and octave's pinv
, is implemented via completely different algorithm than the inv
function. More specifically, singular value decomposition is used, which require's finite-valued matrices (they also can't be sparse
). You didn't say if your matrices are square or not. The real use of pinv
is for solving non-square systems (over- or underdetermined).
但是,无论矩阵的尺寸如何,都不应在应用程序中使用pinv
或inv
.相反,您应该使用mldivide
(八度, Matlab ),即反斜杠运算符\
.这样效率更高,并且在数值上更可靠.
However, you shouldn't be using pinv
or inv
for your application, no matter the dimension of your matrices. Instead you should use mldivide
(octave, Matlab), i.e., the backslash operator, \
. This is much more efficient and numerically robust.
A1 = 3;
A2 = [1 2 1;2 4 6;1 1 3];
A1inv = A1\1
A2inv = A2\eye(size(A2))
mldivide
函数也可以处理矩形矩阵,但是与pinv
相比,对于欠定系统,您将获得不同的答案,因为两个来选择解决方案.
A3 = [1 2 1;2 4 6]; % Underdetermined
A4 = [1 2;2 4;1 1]; % Overdetermined
A3inv = A3\eye(min(size(A3))) % Compare to pinv(A3), different answer
A4inv = A4\eye(max(size(A4))) % Compare to pinv(A4), same answer
如果运行上面的代码,则会发现A3inv
的结果与pinv(A3)
返回的结果略有不同.但是,两者都是有效的解决方案.
这篇关于在Octave/Matlab中pinv([inf])= NaN的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!