简体   繁体   English

在Octave / Matlab中pinv([inf])= NaN的方法

[英]Ways around pinv([inf])=NaN in Octave/Matlab

I am using Octave 3.8.1, a Matlab-like program. 我正在使用类似Matlab的程序Octave 3.8.1。 I'd like to generalize 1/x to the case where x may be a scalar or a matrix. 我想将1/x推广到x可能是标量或矩阵的情况。 Replacing 1/x with inv(x) or pinv(x) works for most x , except: inv(x)pinv(x)替换1/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

Should I convert NaN to 0 afterwards to get this to work? 之后是否应该将NaN转换为0,以使其正常工作? Or have I missed something? 还是我错过了什么? Thanks! 谢谢!

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. Matore和octave的pinv的基础是Moore-Penrose伪逆 ,它是通过与inv函数完全不同的算法实现的。 More specifically, singular value decomposition is used , which require's finite-valued matrices (they also can't be sparse ). 更具体地说,使用奇异值分解 ,它需要使用有限值矩阵(它们也不能是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的真正用途是解决非正方形系统( 欠定 )。

However, you shouldn't be using pinv or inv for your application, no matter the dimension of your matrices. 但是,无论矩阵的尺寸如何,都不应在应用程序中使用pinvinv Instead you should use mldivide ( octave , Matlab ), ie, the backslash operator, \\ . 相反,您应该使用mldivide八度Matlab ),即反斜杠运算符\\ 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))

The mldivide function handles rectangular matrices too, but you will get different answers for underdetermined systems compared to pinv because the two use different methods to choose the solution. 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

If you run the code above, you'll see that you get a slightly different result for A3inv as compared to what is returned by pinv(A3) . 如果运行上面的代码,您将看到与pinv(A3)返回的结果相比, A3inv结果略有不同。 However, both are valid solutions. 但是,两者都是有效的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM