# qr

QR decomposition

### [Q,R] = qr(A)

• A is an m×n matrix, which can be real or complex.
• R is an m×n matrix, and Q is an m×m orthogonal matrix such that A is equal to Q*R.
• R has the following forms:

Eq. (1)

• In the above equation,

• R1 is an n×n upper triangular matrix,
• 0 is an (m-n)×n matrix of zeros,
• R2 is an m×m upper triangular matrix,
• and R3 is an m×(n-m) rectangluar matrix.

Example 1: Factorizing matrices of different sizes give different forms of R.

A=rand(3,5);
[Q,R]=qr(A);
% m < n. Therefore, R has the form [R2 R3]
% where R2 is upper triangular
R
% Q*R and B are the same
% (ignoring rounding errors)
Q*R-A

B=rand(5,3);
[Q,R]=qr(B);
% m > n. Therefore, R has the form [R1;0]
% where R1 is upper triangular
R
% Q*R and B are the same
% (ignoring rounding errors)
Q*R-B

R =
-0.816  -0.792  -1.084  -1.073  -0.449
0.000  -0.187  -0.453  -0.074  -0.589
0.000   0.000   0.795   0.294   0.772

ans = 1e-16 ×
-0.555   1.110   0.555  -2.220   1.171
-1.110   0.000   0.000  -1.110   0.000
0.000  -0.139   0.000   0.000   0.000

R =
-1.529  -0.747  -1.014
0.000  -0.898  -0.565
0.000   0.000  -0.785
0.000   0.000   0.000
0.000   0.000   0.000

ans = 1e-16 ×
0.000   2.776   3.747
1.110   0.000  -2.220
0.000   1.457   1.110
1.110   0.833   1.665
0.000   0.000   0.000


### [Q,R] = qr(A,0)

• A is an m×n matrix, which can be real or complex.
• It performs the same tasks as R=qr(A) and [Q,R]=qr(A) discussed above, but when m > n only returns
• the first n rows of R, i.e., R1 in Eq. (1),
• and the first n columns of Q.
• It returns exactly the same matrices as R=qr(A) and [Q,R]=qr(A) when m <= n.

Example 2: Performing factorization without returning the zero matrix.

A=rand(5,3);
[Q,R]=qr(A);
% R has the form [R1;0]
% where R1 is upper triangular
R

% With the second input argument being 0,
% qr() returns the upper triangular matrix
% without giving the zero matrix
[Q,R1]=qr(A,0);
R1

R =
-1.225  -1.107  -1.083
0.000  -0.801  -0.046
0.000   0.000  -0.364
0.000   0.000   0.000
0.000   0.000   0.000

R1 =
-1.225  -1.107  -1.083
0.000  -0.801  -0.046
0.000   0.000  -0.364


### [Q,R,E] = qr(A, 'matrix')

• A is an m×n matrix, which can be real or complex.
• It gives Q and R together with the permutation matrix E.
• Q is an m×m orthogonal matrix, R is m×n, and E is an n×n permutation matrix such that A*E is equal to Q*R and the diagonal elements of R are arranged in decreasing order of their absolute values (or moduli if A is complex).
• R has the same form as R shown in Eq. (1) above.
A=rand(3,5);
[Q,R,E]=qr(A,'matrix');
% A*E and Q*R are the same
% (ignoring rounding errors)
A*E-Q*R

ans = 1e-16 ×
1.110  -4.441  -0.694  -3.331  -1.665
-1.110  -2.498   0.555  -1.110  -1.110
0.000  -1.110   0.000   0.278   1.110


### [Q,R,e] = qr(A,0)

• A is an m×n matrix, which can be real or complex.
• It performs the same tasks as [Q,R,E]=qr(A) and [Q,R,E]=qr(A,'matrix') discussed above, but when m > n only returns
• the first m rows of R, i.e., R1 in Eq. (1),
• the first m columns of Q,
• and a permutation vector e.
• It returns exactly the same matrices as [Q,R,E]=qr(A) and [Q,R,E]=qr(A,'matrix') when m <= n.

### [Q,R,e] = qr(A, 'vector')

• A is an m×n matrix, which can be real or complex.
• It returns the same Q and R as in [Q,R,E]=qr(A) and [Q,R,E]=qr(A,'matrix') discussed above. But, instead of returning a permutation matrix, it returns a vector e containing permutation information. The following example shows how the permuation matrix E can be obtained from e.

Example 3: Obtaining a permutation matrix from the vector e.

% Randomly generated matrix
a=randi(10,4,5);

% Vector e contains permutation information
[Q,R,e]=qr(a,'vector');

% Construct a permuation matrix E using e
E=zeros(size(a,2));
for r=1:size(a,2)
E(e(r),r)=1;
end

% Permutation matrix E2 given by qr()
% with second input argument being 'matrix'
[Q,R,E2]=qr(a,'matrix');

% Permutation matrices E and E2 are the same
E
E2

E =
0.000   1.000   0.000   0.000   0.000
0.000   0.000   0.000   1.000   0.000
1.000   0.000   0.000   0.000   0.000
0.000   0.000   0.000   0.000   1.000
0.000   0.000   1.000   0.000   0.000

E2 =
0.000   1.000   0.000   0.000   0.000
0.000   0.000   0.000   1.000   0.000
1.000   0.000   0.000   0.000   0.000
0.000   0.000   0.000   0.000   1.000
0.000   0.000   1.000   0.000   0.000