% Rectangular A

m=4;
n=3;%m>n
A=rand(m,n)

%
% Full QR-factorization

[Qf,Rf]=qr(A)
%check that Qf is orthogonal
Qf^(-1)
Qf'
Qf^(-1)-Qf'
qf1=Qf(:,1)
qf3=Qf(:,3)
%inner product of any two colunm of Q is zero
dot(qf1,qf3) %1st way to calculate inner product
qf3'*qf1 %2nd way to calculate inner product
norm(qf1)
norm(qf3)

%
% Thin QR-factorization

[Qt,Rt]=qr(A,0)
disp('Qf='),disp(Qf)
disp('Rf='),disp(Rf)

%
% Check that both Qf, Rf and Qt, Rt are factorizations of A

A
disp('Qf*Rf='), Qf*Rf
disp('A-Qf*Rf='), A-Qf*Rf
disp('Qt*Rt='), Qt*Rt
disp('A-Qt*Rt='),A-Qt*Rt
disp('Qf*Rf-Qt*Rt='),Qf*Rf-Qt*Rt

%
% Square A: thin and full QR-factorizations are the same

A=rand(n)
[Qf,Rf]=qr(A)
[Qt,Rt]=qr(A,0)

%
% Solve linear system Ax=b using QR-factorization

A=rand(n,n)
b=rand(n,1)
x=A\b
r=b-A*x

%

[Q,R]=qr(A);
x_qr=R\(Q'*b)
r_qr=b-A*x_qr
x-x_qr