home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / MATFUN.DI$ / QRINSERT.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.1 KB  |  41 lines

  1. function [Q,R] = qrinsert(Q,R,j,x)
  2. %QRINSERT Insert a column in the QR factorization.
  3. %    If [Q,R] = qr(A) is the original QR factorization of A,
  4. %    then [Q,R] = qrinsert(Q,R,j,x) changes Q and R to be the
  5. %    factorization of the matrix obtained by inserting an extra
  6. %    column, x, before A(:,j).  (If A has n columns and j = n+1,
  7. %    then x is inserted after the last column of A.)
  8. %
  9. %    See also QR, QRDELETE, PLANEROT.
  10.  
  11. %    C.B.Moler 5/1/92.  Revised ACWG 6/15/92, CBM 9/11/92.
  12. %    Copyright (c) 1984-93 by the MathWorks, Inc.
  13.  
  14. [m,n] = size(R);
  15. if n == 0
  16.     [Q,R] = qr(x);
  17.     return;
  18. end
  19. % Make room and insert x before j-th column.
  20. R(:,j+1:n+1) = R(:,j:n);
  21. R(:,j) = Q'*x;
  22. n = n+1;
  23.  
  24. % Now R has nonzeros below the diagonal in the j-th column,
  25. % and "extra" zeros on the diagonal in later columns.
  26. %    R = [x x x x x
  27. %         0 x x x x
  28. %         0 0 + x x
  29. %         0 0 + 0 x
  30. %         0 0 + 0 0]
  31. % Use Givens rotations to zero the +'s, one at a time, from bottom to top.
  32.  
  33. for k = m-1:-1:j
  34.    p = k:k+1;
  35.    [G,R(p,j)] = planerot(R(p,j));
  36.    if k < n
  37.       R(p,k+1:n) = G*R(p,k+1:n);
  38.    end
  39.    Q(:,p) = Q(:,p)*G';
  40. end
  41.