Computes the QR factorization of A via Householder reflections explicitly calculating the matrix of the Householder reflection Input arguments: A, matrix Output arguments: Q, square orthogonal matrix R, upper triangular
0001 function [ Q,R ] = QR_Householder_1( A ) 0002 % Computes the QR factorization of A via Householder reflections 0003 % explicitly calculating the matrix of the Householder reflection 0004 % Input arguments: 0005 % A, matrix 0006 % Output arguments: 0007 % Q, square orthogonal matrix 0008 % R, upper triangular 0009 0010 [n,m]=size(A); % finding the size of A 0011 0012 R=A; Q=eye(n); % initialize Q and R 0013 0014 % considering the columns in turn 0015 for j=1:min(n,m); 0016 u=R(:,j); % initialize vector u to be jth column 0017 for i=1:j-1; % set first j-1 entries of u to zero 0018 u(i)=0; 0019 end 0020 if u(j) == 0 0021 u(j) = u(j)+norm(u); 0022 else 0023 u(j)=u(j)+sign(u(j))*norm(u); % set jth entry of u avoiding 0024 % loss of significance 0025 end 0026 if norm(u) == 0 0027 H=eye(n); 0028 else 0029 H=eye(n)-(2/norm(u)^2)*(u*u') ; % form Householder reflection 0030 end 0031 R=H*R; % apply reflection 0032 Q=Q*H'; % update Q by right multiplying by H transpose 0033 end 0034 end 0035 0036