projectionmatrix.m 854 B

123456789101112131415161718192021222324252627282930313233
  1. function f = projectionmatrix(X)
  2. % function f = projectionmatrix(X)
  3. %
  4. % <X> is samples x parameters
  5. %
  6. % what we want to do is to perform a regression using <X>
  7. % and subtract out the fit. this is accomplished by
  8. % y-X*inv(X'*X)*X'*y = (I-X*inv(X'*X)*X')*y = f*y
  9. % where y is the data (samples x cases).
  10. %
  11. % what this function does is to return <f> which has
  12. % dimensions samples x samples. to accomplish this,
  13. % we rely heavily on olsmatrix.m.
  14. %
  15. % if <X> has no parameters, the output of this function is 1.
  16. %
  17. % history:
  18. % - 2013/08/18 - in the cases of empty <X>, we now return 1 instead of [].
  19. %
  20. % example:
  21. % x = sort(randn(100,1));
  22. % x2 = projectionmatrix(constructpolynomialmatrix(100,0:1))*x;
  23. % figure; hold on; plot(x,'r-'); plot(x2,'g-');
  24. % handle corner case
  25. if isempty(X)
  26. f = 1;
  27. return;
  28. end
  29. % do it
  30. f = eye(size(X,1)) - X*olsmatrix(X);