time | calls | line |
---|
| | 1 | function X = pinv(A,tol)
|
| | 2 | %PINV Pseudoinverse.
|
| | 3 | % X = PINV(A) produces a matrix X of the same dimensions
|
| | 4 | % as A' so that A*X*A = A, X*A*X = X and A*X and X*A
|
| | 5 | % are Hermitian. The computation is based on SVD(A) and any
|
| | 6 | % singular values less than a tolerance are treated as zero.
|
| | 7 | % The default tolerance is MAX(SIZE(A)) * NORM(A) * EPS(class(A)).
|
| | 8 | %
|
| | 9 | % PINV(A,TOL) uses the tolerance TOL instead of the default.
|
| | 10 | %
|
| | 11 | % Class support for input A:
|
| | 12 | % float: double, single
|
| | 13 | %
|
| | 14 | % See also RANK.
|
| | 15 |
|
| | 16 | % Copyright 1984-2013 The MathWorks, Inc.
|
| | 17 |
|
0.04 | 20 | 18 | [U,S,V] = svd(A,'econ');
|
| 20 | 19 | s = diag(S);
|
| 20 | 20 | if nargin < 2
|
| | 21 | tol = max(size(A)) * eps(norm(s,inf));
|
| | 22 | end
|
| 20 | 23 | r1 = sum(s > tol)+1;
|
| 20 | 24 | V(:,r1:end) = [];
|
| 20 | 25 | U(:,r1:end) = [];
|
| 20 | 26 | s(r1:end) = [];
|
| 20 | 27 | s = 1./s(:);
|
| 20 | 28 | X = bsxfun(@times,V,s.')*U';
|