spm_inv.m 911 B

123456789101112131415161718192021222324252627282930
  1. function X = spm_inv(A,TOL)
  2. % inverse for ill-conditioned matrices
  3. % FORMAT X = spm_inv(A,TOL)
  4. %
  5. % A - matrix
  6. % X - inverse
  7. %
  8. % TOL - tolerance: default = max(eps(norm(A,'inf'))*max(m,n),exp(-32))
  9. %
  10. % This routine simply adds a small diagonal matrix to A and calls inv.m
  11. %__________________________________________________________________________
  12. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  13. % Karl Friston
  14. % $Id: spm_inv.m 7143 2017-07-29 18:50:38Z karl $
  15. % check A
  16. %--------------------------------------------------------------------------
  17. [m,n] = size(A);
  18. if isempty(A), X = sparse(n,m); return, end
  19. % tolerance
  20. %--------------------------------------------------------------------------
  21. if nargin == 1
  22. TOL = max(eps(norm(A,'inf'))*max(m,n),exp(-32));
  23. end
  24. % inverse
  25. %--------------------------------------------------------------------------
  26. X = inv(A + speye(m,n)*TOL);