function ShowQRP()
% Illustrates the QR-with-column-pivoting factorization AP = QR

clc
format short
% Generate a small nearly rank-deficient example...
m = 8;
n = 5;
r = 3;
noise = .001;
A = randn(m,r)*randn(n,r)' + noise*randn(m,n)
% A is mxn and close to a rank-r matrix.
[Q,R,piv] = qr(A,0)
I = eye(n,n);
P = I(:,piv)
fprintf('\n|| A*P - Q*R || = %10.3e\n',norm(A*P-Q*R))
fprintf('\n\nThe columns of A(:,piv(1:r)) are strongly independent.\n')
fprintf(' cond(A(:,piv(1:r))) = %5.3f\n',cond(A(:,piv(1:r))))