MATLAB: How to compute only the matrix Q in QR factorization in MATLAB 7.10 (R2010a)

MATLAB

I have a large, sparse m-by-n matrix A where n >> m. I would like to compute only the matrix Q in the QR factorization of A. I have enough memory to store the m-by-m matrix Q, but I do not have enough memory to compute and store the m-by-n matrix R.

Best Answer

In QR factorization of an m-by-n matrix A where n > m, only the first m columns of A are needed to compute Q. Therefore, you can obtain Q from the smaller m-by-m matrix formed from the first m columns of A. The following small example demonstrates this idea:
m = 40;
n = 100;
A = rand(m,n);
[Q, ~] = qr(A);
Asmall = A(:,1:m); % left m-by-m block from A
[Q2, ~] = qr(Asmall);
% check that Q, Q2 are equal to within roundoff error
err = norm(Q - Q2)
Note that this approach will work even if A is full rank but 'Asmall' is not full rank. In general, the QR decomposition is not unique. Even in the case of 'Asmall' not being full rank, Q will still be unitary, and there will still exist an R such that Q*R = A for the given Q matrix. The first m columns of such an R are computed (although we are not interested in them). The m+1:n columns of R will not be computed, but there is no doubt about their existence. They will be whatever they need to be so that Q*R=A.