如何生成随机可逆对称正半定矩阵?

时间:2018-09-04 07:19:41

标签: matlab matrix random matrix-inverse

如何使用MATLAB生成随机,可逆,对称,正半定矩阵?

我找到了以下Python代码:

matrixSize = 10
A = random.rand(matrixSize,matrixSize)
B = numpy.dot(A,A.transpose())

但是我不确定这是否会生成随机正半定矩阵B

2 个答案:

答案 0 :(得分:2)

The MATLAB equivalent of your code is:

matrixSize = 10;
A = rand(matrixSize);
B = A * A.';

This does produce a symmetric, positive-semidefinite matrix. But this matrix is not necessarily invertible, it is possible (though very unlikely) that the matrix is singular. More likely is that it is almost singular, meaning that the inverse will get very large values. This inverse is imprecise, and B*inv(B) will be different from the identity matrix by an amount larger than your tolerance.

One simple way of ensuring that B*inv(B) is within tolerance of the identity matrix is to repeatedly generate a random matrix until you find one that is OK:

tol = 1e-12;
while true
   A = rand(matrixSize);
   B = A*A.';
   err = abs(B*inv(B) - eye(matrixSize));
   if all(err(:)<tol)
      break
   end
end

The loop above will run only once most of the time, only occasionally will it need to generate a second matrix.

答案 1 :(得分:2)

对于任何eps> 0和任何nxk(对于任何k)矩阵B,矩阵

P = eps*I + B*B'

是肯定的和可逆的。 如果k


MATLAB代码以获得P

n = 10;
k = 1;
B = rand(n,k);
B = B * B.';
P = B + eye(size(B)) * eps(max(B(:)));