function [Q,R]=AddRowQR(Q,R,k,w);
% ADDROWQR QR decomposition of modified matrix
%   [Q,R]=AddRowQR(Q,R,k,w); Given [Q,R]=qr(A),computes the QR
%   decomposition for [A(1,:);A(2,:), ...,A(k-1,:);w;A(k,:),...A(n,:)]
    
[p,n]=size(R); m=size(Q,1);                 % Q is (m x p)
Q=[Q(1:k-1,:), zeros(k-1,1)                 % add row and column to Q
   zeros(1,p), 1                            % and permute
   Q(k:m,:), zeros(m-k+1,1)];
R=[R; w];                                   % augment R
k=p+1;
for j=1:min(n,p)
  G=planerot(R(j:k-j:k,j));         
  R(j:k-j:k,j:n)=G*R(j:k-j:k,j:n);          % annihilate w
  Q(:,j:k-j:k)=Q(:,j:k-j:k)*G';             % update Q
end
