Updated March 10, 2023

## Introduction to Matlab QR

The following article provides an outline for Matlab QR. QR factorization is used in mathematics to decompose a matrix and express it into a product of 2 different matrices named ‘Q’ and ‘R’. The matrix ‘Q’ is called ‘orthogonal matrix’, and ‘R’ is called ‘right triangular matrix’.

If ‘A’ is our input matrix, then QR matrix decomposes as below equation:

QR factorization is used in Data Science, Data Analysis and Statistics. For example, it is very useful in computing the solution of the Least Square Problem.

**Syntax of QR Function**

Given below is the syntax mentioned:

`X = qr (A)`

`QR = qr (A)`

**Explanation:**

- X = qr (A) is used to return the upper-triangular factor, i.e. ‘R’ matrix. If input ‘A’ is a full matrix, then the upper right triangular matrix ‘R’ is given by ‘triu (X)’; however, if A is a sparse matrix, then R will be equal to ‘X’.
- [Q, R] = qr (A) is used to get QR decomposition of the input matrix A. In the output, ‘R’ will be right triangular, and ‘Q’ will be an orthogonal matrix.

### Examples of Matlab QR

Given below are the examples of Matlab QR:

#### Example #1

This example will use the QR function to compute the upper triangular matrix of a 4 x 5 matrix.

Below are the steps to be followed:

- First, initialize the 4 x 5 input matrix.
- Pass this input matrix as an argument to the qr function.
- Get the upper triangular matrix ‘R’ using the function ‘triu’.

**Code:**

A = [2 4 1 4 2; 1 -1 3 0 2; 2 11 6 0 -2; 0 1 2 3 1];

[Initializing the 4 x 5 input matrix]

X = qr (A)

[Using the qr function to compute the qr decomposition of the input matrix]

R = triu (X)

[Using the triu function to compute the upper triangular matrix]

**Input:**

**Output:**

**(Matrix X):**

**(Upper Triangular Matrix):**

As we can see in the output, the qr function has provided the qr decomposition of the input matrix. Also, the triu function has provided us with the upper triangular matrix.

#### Example #2

This example will use the QR function to compute the upper triangular matrix of a 5 x 5 matrix.

Below are the steps to be followed:

- First, initialize the 5 x 5 input matrix.
- Pass this input matrix as an argument to the qr function.
- Get the upper triangular matrix ‘R’ using the function ‘triu’.

**Code:**

A = [1 -4 3 4 6; 1 3 3 10 2; 3 1 4 0 4; 3 5 2 1 -1; 3 5 0 1 2];

[Initializing the 5 x 5 input matrix]

X = qr (A)

[Using the qr function to compute the qr decomposition of the input matrix]

R = triu (X)

[Using the triu function to compute the upper triangular matrix]

**Input:**

**Output:**

**(Matrix X):**

**(Upper Triangular Matrix):**

As we can see in the output, the qr function has provided the qr decomposition of the input matrix. Also, the triu function has provided us with the upper triangular matrix.

In the above 2 examples, we used the qr function and triu function to get the upper triangular matrix of our input matrix.

Next, we will see how to get both the upper triangular and orthogonal matrices using the qr function.

#### Example #3

This example will use the QR function to compute the upper triangular matrix and the orthogonal matrix of a 5 x 5 matrix.

Below are the steps to be followed:

- First, initialize the 5 x 5 input matrix.
- Pass this input matrix as an argument to the qr function.

**Code:**

A = [3 -4 -4 7 6; 11 7 13 10 8; 2 11 14 1 8; 13 15 2 7 -1; 6 15 10 11 2];

[Initializing the 5 x 5 input matrix]
[Q, R] = qr (A)

[Using the qr function to compute the qr decomposition of the input matrix]

**Input:**

**Output:**

**(Matrix Q):**

**(Matrix R):**

As we can see in the output, the qr function has provided the qr decomposition of the input matrix.

#### Example #4

This example will use the QR function to compute the upper triangular matrix and the orthogonal matrix of a 4 x 4 matrix.

Below are the steps to be followed:

- First, initialize the 4 x 4 input matrix.
- Pass this input matrix as an argument to the qr function.

**Code:**

A = [3 4 0 7; 1 4 3 10; 11 4 1 8; 13 15 2 7];

[Initializing the 4 x 4 input matrix]
[Q, R] = qr (A)

[Using the qr function to compute the qr decomposition of the input matrix]

**Input:**

**Output:**

**(Matrix Q):**

**(Matrix R):**

As we can see in the output, the qr function has provided the qr decomposition of the input matrix.

### Conclusion

The qr function is used in Matlab to get the qr decomposition of the input matrix. It can be used to get both the upper triangular matrix and the orthogonal matrix. It is widely used to solve the least square problems in data science.

### Recommended Articles

This is a guide to Matlab QR. Here we discuss the introduction and the examples of Matlab QR for a better understanding. You may also have a look at the following articles to learn more –