function B = getbasis(M) %GETBASIS Orders the columns of M to serve as a good basis for % the column space of M itself. This is accomplished using % a computationally intense, iterative algorithim based on the % Singular Value Decomposition (SVD). The first column of B % is most significant the second next so, etc. in descending % order. % % Written by Finny Kuruvilla v1.1.0 Last updated 5-14-2001 [rows,cols]=size(M); if rank(M)< min(rows,cols) error('Matrix is rank deficient.'); end A=M; for k=1:cols [U,D,V]=svd(A); % compute the SVD of our running matrix A. c=U(:,1); % use the first column of U to find our next column vector. coeffs=M\c; % find the least-squares solution for which column of % M contributes most to the first column of U [maxval,pos]=max(abs(coeffs)); B(:,k)=M(:,pos); % grab the column of M that has highest abs least-squares coefficient. M(:,pos)=[]; % and delete it from M. P=B*inv(B'*B)*B'; % construct the projection matrix of existing basis vectors. A=M-P*M; % and use it to remove the span of the existing basis from M. disp(sprintf('Basis %d of %d computed',k,cols)); pause(.01); end