function Mar6()
% Underdetermined systems via QR
%
clc
format short
m = 4; n = 7;
A = randn(m,n); b = randn(m,1);
[Q,R] = qr(A')
% Since A' = QR, A = R'*Q' and so b = Ax = R'*Q'x = R'y where y = Q'x.
% Now R' = [R(1:m,1:m)' zeros(m,n-m)] so 
%          b = R'y = R(1:m,1:m)'*y(1:m) + zeros(m,n-m)*y(m+1:n)
% Thus, y(1:m) = R(1:m,1:m)'\b and  y(m+1:n) can be anything so set it
% to zero...
y = [R(1:m,1:m)'\b ; zeros(n-m,1)]
% Since Q is orthogonal an y = Q'x,
x = Q*y
% Check:
err = norm(A*x -b)