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:
Condition number (Maximal singular value/Minimal singular value)
is another good method:
cond(A)
It uses svd
. It should be as close to 1 as possible. Very large values mean that the matrix is almost singular. Inf
means that it is precisely singular.
Note that almost all of the methods mentioned in other answers use somehow svd
:
There are special tools designed for this problem, appropriately called "rank revealing matrix factorizations". To my best (albeit a little old) knowledge, a good enough way to decide whether a n x n
matrix A
is nonsingular is to go with
det(A) <> 0 <=> rank(A) = n
and use a rank-revealing QR factorization of A:
AP = QR
where Q
is orthogonal, P
is a permutation matrix and R
is an upper triangular matrix with the property that the magnitude of the diagonal elements is decreased along the diagonal.
You could compare the result of rank(G)
with the number of columns of G
. If the rank is less than the column dimension, you will have a singular matrix.
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
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.
you can also check this by:
min(svd(A))>eps
and verifying that the smallest singular value is larger than eps, or any other numerical tolerance that is relevant to your needs. (the code will return 1 or 0)
Here's more info about it...