代码分析器:INV缓慢且不准确 [英] Code Analyzer: INV is slow and inaccurate

查看:103
本文介绍了代码分析器:INV缓慢且不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Matlab的inv()操作计算矩阵逆时:

When I try to calculate a matrix inverse using Matlab's inv() operation:

A = rand(10,10);
b = rand(10,1);

C = inv(A);
D = C*b;

我在最后一行收到以下警告:INV缓慢且不准确.对于INV(A)* b使用A \ b,对于b * INV(A)使用b/A.

I get the following warning at the last row: INV is slow and inaccurate. Use A\b for INV(A)*b , and b/A for b*INV(A).

我可以将上面的代码更改为:

I can change the code above into:

A = rand(10,10);
b = rand(10,1);

C = inv(A);
D = A\b;

现在我没有得到警告,但是我认为这种解决方案不是更好的选择.

Now I don't get the warning, but I don't think this solution is better.

注意:我需要同时存储矩阵A的逆和inv(A)* c.另外,在我的真实文件中,矩阵A的大小可以为5000 x 5000甚至更大.

Note: I need to store both the inverse of matrix A as well as inv(A)*c. Also, in my real file the size of matrix A can be 5000 x 5000 or even bigger.

在效率和准确性方面是否有更好的解决方案,或者第一种方法还可以吗?

Are there any better solutions in terms of efficiency and accuracy or is the first method fine?

推荐答案

您应该听Matlab并使用第二个选项. inv(A)*bA\b是在后台使用不同的算法计算的,而\确实更准确.

You should listen to Matlab and use the second option. inv(A)*b and A\b are computed with different algorithms under the hood, and \ is indeed more accurate.

inv文档说明:

The documentation for inv states:

在实践中,几乎不需要形成矩阵的显式逆.当求解线性方程组Ax = b时,会经常滥用inv.解决此问题的一种方法是x = inv(A)* b.从执行时间和数值精度的角度来看,更好的方法是使用矩阵除法运算符x = A \ b.这将使用高斯消去法生成解决方案,而不会形成逆函数.有关更多信息,请参见mldivide().

In practice, it is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve this is with x = inv(A)*b. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator x = A\b. This produces the solution using Gaussian elimination, without forming the inverse. See mldivide () for further information.

这篇关于代码分析器:INV缓慢且不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆