本文介绍了在Octave/Matlab中pinv([inf])= NaN的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用类似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).

但是,无论矩阵的尺寸如何,都不应在应用程序中使用pinvinv.相反,您应该使用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相比,对于欠定系统,您将获得不同的答案,因为两个来选择解决方案.

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.

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)返回的结果略有不同.但是,两者都是有效的解决方案.

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). However, both are valid solutions.

这篇关于在Octave/Matlab中pinv([inf])= NaN的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多