如何在Matlab中查找矩阵是否为奇异 [英] How to find if a matrix is Singular in Matlab
问题描述
我使用下面的函数从优化器中为给定的一组猜测lambda生成beta.
I use the function below to generate the betas for a given set of guess lambdas from my optimiser.
运行时,我经常收到以下警告消息:
When running I often get the following warning message:
警告:矩阵对于工作精度是唯一的. 在9的NSS_betas中 在德拉姆达(Dlambda),19岁 在36岁的Individual_Lambdas中
Warning: Matrix is singular to working precision. In NSS_betas at 9 In DElambda at 19 In Individual_Lambdas at 36
我希望能够从解决方案集中排除形成奇异矩阵的任何beta,但是我不知道如何进行测试?
I'd like to be able to exclude any betas that form a singular matrix form the solution set, however I don't know how to test for it?
我一直在尝试使用rcond(),但是我不知道在单数和非单数之间进行截断的位置是什么?
I've been trying to use rcond() but I don't know where to make the cut off between singular and non singular?
当然,如果Matlab生成警告消息,它已经知道矩阵是否为奇异,所以如果我能找到该变量的存储位置,我可以使用它吗?
Surely if Matlab is generating the warning message it already knows if the matrix is singular or not so if I could just find where that variable was stored I could use that?
function betas=NSS_betas(lambda,data)
mats=data.mats2';
lambda=lambda;
yM=data.y2';
nObs=size(yM,1);
G= [ones(nObs,1) (1-exp(-mats./lambda(1)))./(mats./lambda(1)) ((1-exp(-mats./lambda(1)))./(mats./lambda(1))-exp(-mats./lambda(1))) ((1-exp(-mats./lambda(2)))./(mats./lambda(2))-exp(-mats./lambda(2)))];
betas=G\yM;
r=rcond(G);
end
感谢您的建议:
在将lambda值设置为相等后,我测试了下面的所有三个示例,因此求出了奇异矩阵
I tested all three examples below after setting the lambda values to be equal so guiving a singular matrix
if (~isinf(G))
r=rank(G);
r2=rcond(G);
r3=min(svd(G));
end
r = 3,r2 = 2.602085213965190e-16; r3 = 1.075949299504113e-15;
r=3, r2 =2.602085213965190e-16; r3= 1.075949299504113e-15;
因此,在此测试中,rank()和rcond()假设我采用下面给出的基准值,则可以正常工作.
So in this test rank() and rcond () worked assuming I take the benchmark values as given below.
但是当我有两个接近但不完全相等的值时会发生什么?
However what happens when I have two values that are close but not exactly equal?
如何确定太近的地方?
推荐答案
rcond
是前往此处的正确方法.如果它接近机器精度零,则您的矩阵是奇异的.我通常会选择:
rcond
is the right way to go here. If it nears the machine precision of zero, your matrix is singular. I usually go with:
if( rcond(A) < 1e-12 )
% This matrix doesn't look good
end
您可以根据自己的需要尝试使用一个值,但是使用MATLAB甚至接近奇异值的矩阵求逆也会产生垃圾结果.
You can experiment with a value that suites your needs, but taking the inverse of a matrix that is even close to singular with MATLAB can produce garbage results.
这篇关于如何在Matlab中查找矩阵是否为奇异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!