Computes the QR factorization of A via Householder reflections avoids calculating the matrix of the Householdre reflection Input arguments: A, matrix Output arguments: Q, square orthogonal matrix R, upper triangular
0001 function [ Q,R ] = QR_Householder_2( A ) 0002 % Computes the QR factorization of A via Householder reflections 0003 % avoids calculating the matrix of the Householdre 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 y=u'*R; % form matrix-vector product 0028 R=R-(2/norm(u)^2)*u*y; % form R 0029 0030 z=u'*Q; % perform same operation on Q 0031 Q=Q-(2/norm(u)^2)*u*z; 0032 end 0033 end 0034 Q=Q'; % reassign Q to its transpose 0035 end