注:X = pinv(A,tol)指定容差的值。pinv
将 A
中小于容差的奇异值视为零。
function X = pinv(A,tol)
%PINV Pseudoinverse.
% X = PINV(A) produces a matrix X of the same dimensions
% as A' so that A*X*A = A, X*A*X = X and A*X and X*A
% are Hermitian. The computation is based on SVD(A) and any
% singular values less than a tolerance are treated as zero.
%
% PINV(A,TOL) treats all singular values of A that are less than TOL as
% zero. By default, TOL = max(size(A)) * eps(norm(A)).
%
% Class support for input A:
% float: double, single
%
% See also RANK.
% Copyright 1984-2015 The MathWorks, Inc.
[U,S,V] = svd(A,'econ');
s = diag(S);
if nargin < 2
tol = max(size(A)) * eps(norm(s,inf));
end
r1 = sum(s > tol)+1;
V(:,r1:end) = [];
U(:,r1:end) = [];
s(r1:end) = [];
s = 1./s(:);
X = (V.*s.')*U';