我正在寻找Moore-Penrose算法计算伪逆矩阵的Matlab实现。
我尝试了几个算法,这个算法
http://arxiv.org/ftp/arxiv/papers/0804/0804.4809.pdf
第一眼看上去很好看。然而,问题是,对于大型元素,它会产生严重缩放的矩阵,并且一些内部操作会失败。它涉及以下步骤:
L=L(:,1:r);
M=inv(L'*L);
我正在尝试找到一个更强大的解决方案,这个解决方案可以在我的其他SW中轻松实现。谢谢你的帮助。
答案 0 :(得分:4)
使用内置pinv
?
否则,您可以查看implementation used in Octave。它不是Octave / MATLAB语法,但我想你应该能够轻松移植它。
答案 1 :(得分:4)
我使用Lutz Roeder的Mapack矩阵库在C#中重新实现了一个。也许这个或Java版本对您有用。
/// <summary>
/// The difference between 1 and the smallest exactly representable number
/// greater than one. Gives an upper bound on the relative error due to
/// rounding of floating point numbers.
/// </summary>
const double MACHEPS = 2E-16;
// NOTE: Code for pseudoinverse is from:
// http://the-lost-beauty.blogspot.com/2009/04/moore-penrose-pseudoinverse-in-jama.html
/// <summary>
/// Computes the Moore–Penrose pseudoinverse using the SVD method.
/// Modified version of the original implementation by Kim van der Linde.
/// </summary>
/// <param name="x"></param>
/// <returns>The pseudoinverse.</returns>
public static Matrix MoorePenrosePsuedoinverse(Matrix x)
{
if (x.Columns > x.Rows)
return MoorePenrosePsuedoinverse(x.Transpose()).Transpose();
SingularValueDecomposition svdX = new SingularValueDecomposition(x);
if (svdX.Rank < 1)
return null;
double[] singularValues = svdX.Diagonal;
double tol = Math.Max(x.Columns, x.Rows) * singularValues[0] * MACHEPS;
double[] singularValueReciprocals = new double[singularValues.Length];
for (int i = 0; i < singularValues.Length; ++i)
singularValueReciprocals[i] = Math.Abs(singularValues[i]) < tol ? 0 : (1.0 / singularValues[i]);
Matrix u = svdX.GetU();
Matrix v = svdX.GetV();
int min = Math.Min(x.Columns, u.Columns);
Matrix inverse = new Matrix(x.Columns, x.Rows);
for (int i = 0; i < x.Columns; i++)
for (int j = 0; j < u.Rows; j++)
for (int k = 0; k < min; k++)
inverse[i, j] += v[i, k] * singularValueReciprocals[k] * u[j, k];
return inverse;
}
答案 2 :(得分:1)
这是R代码[I] [1]编写的用于计算M-P伪逆的代码。我认为这很简单,可以翻译成matlab代码。
pinv<-function(H){
x=t(H) %*% H
s=svd(x)
xp=s$d
for (i in 1:length(xp)){
if (xp[i] != 0){
xp[i]=1/xp[i]
}
else{
xp[i]=0
}
}
return(s$u %*% diag(xp) %*% t(s$v) %*% t(H))
}