function [Q, R] = gs(A); % % Gram-Schmidt. this is not a stable way to compute a % qr factorization % written by Ming Gu for Math 221, Fall 2007 % [m, n] = size(A); % % we assume that m>= n. % V = A; R = zeros(n,n); for j=1:n vj = A(:,j); if (j > 1) for i = 1:j-1 R(i,j) = Q(:,i)' * A(:,j); vj = vj - R(i,j) * Q(:,i); end end R(j,j) = norm(vj); Q(:,j) = vj / R(j,j); end