Octave/Matlab 中 pinv([inf])=NaN 的解决方法
Ways around pinv([inf])=NaN in Octave/Matlab
我正在使用 Octave 3.8.1,一个类似 Matlab 的程序。我想将 1/x
概括为 x
可能是标量或矩阵的情况。将 1/x
替换为 inv(x)
或 pinv(x)
适用于大多数 x
,除了:
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 才能使其正常工作?或者我错过了什么?谢谢!
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
(octave, Matlab),即反斜杠运算符 \
。这样效率更高,并且在数值上更稳健。
A1 = 3;
A2 = [1 2 1;2 4 6;1 1 3];
A1inv = A1
A2inv = A2\eye(size(A2))
mldivide
函数也处理矩形矩阵,但与 pinv
相比,对于欠定系统你会得到不同的答案,因为两个 use different methods 来选择解决方案。
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 3.8.1,一个类似 Matlab 的程序。我想将 1/x
概括为 x
可能是标量或矩阵的情况。将 1/x
替换为 inv(x)
或 pinv(x)
适用于大多数 x
,除了:
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 才能使其正常工作?或者我错过了什么?谢谢!
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
(octave, Matlab),即反斜杠运算符 \
。这样效率更高,并且在数值上更稳健。
A1 = 3;
A2 = [1 2 1;2 4 6;1 1 3];
A1inv = A1
A2inv = A2\eye(size(A2))
mldivide
函数也处理矩形矩阵,但与 pinv
相比,对于欠定系统你会得到不同的答案,因为两个 use different methods 来选择解决方案。
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)
的返回结果略有不同。但是,两者都是有效的解决方案。