home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / MATFUN.DI$ / PINV.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  746 b   |  30 lines

  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 AX and XA
  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.
  8. %    This tolerance may be overridden with X = PINV(A,tol).
  9. %
  10. %    See also RANK.
  11.  
  12. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  13.  
  14. [U,S,V] = svd(A,0);
  15. if min(size(S)) == 1
  16.    S = S(1);
  17. else
  18.    S = diag(S);
  19. end
  20. if (nargin == 1)
  21.    tol = max(size(A)) * S(1) * eps;
  22. end
  23. r = sum(S > tol);
  24. if (r == 0)
  25.    X = zeros(size(A'));
  26. else
  27.    S = diag(ones(r,1)./S(1:r));
  28.    X = V(:,1:r)*S*U(:,1:r)';
  29. end
  30.