spm_orth.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function X = spm_orth(X,OPT)
  2. % Recursive Gram-Schmidt orthogonalisation of basis functions
  3. % FORMAT X = spm_orth(X,OPT)
  4. %
  5. % X - matrix
  6. % OPT - 'norm' for Euclidean normalisation
  7. % - 'pad' for zero padding of null space [default]
  8. %
  9. % Serial orthogonalisation starting with the first column
  10. %
  11. % Reference:
  12. % Golub, Gene H. & Van Loan, Charles F. (1996), Matrix Computations (3rd
  13. % ed.), Johns Hopkins, ISBN 978-0-8018-5414-9.
  14. %__________________________________________________________________________
  15. % Copyright (C) 2002-2012 Wellcome Trust Centre for Neuroimaging
  16. % Karl Friston
  17. % $Id: spm_orth.m 5821 2013-12-31 14:26:41Z karl $
  18. %-Default
  19. %--------------------------------------------------------------------------
  20. try
  21. OPT;
  22. catch
  23. OPT = 'pad';
  24. end
  25. %-Recursive Gram-Schmidt orthogonalisation
  26. %--------------------------------------------------------------------------
  27. sw = warning('off','all');
  28. [n,m] = size(X);
  29. X = X(:, any(X));
  30. rankX = rank(full(X));
  31. try
  32. x = X(:,1);
  33. j = 1;
  34. for i = 2:size(X, 2)
  35. D = X(:,i);
  36. D = D - x*(pinv(x)*D);
  37. if norm(D,1) > exp(-32)
  38. x = [x D];
  39. j(end + 1) = i;
  40. end
  41. if numel(j) == rankX, break, end
  42. end
  43. catch
  44. x = zeros(n,0);
  45. j = [];
  46. end
  47. warning(sw);
  48. % and normalisation, if requested
  49. %--------------------------------------------------------------------------
  50. switch OPT
  51. case{'pad'}
  52. X = zeros(n,m);
  53. X(:,j) = x;
  54. case{'norm'}
  55. X = spm_en(x);
  56. otherwise
  57. X = x;
  58. end