EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab QR
 

Matlab QR

Updated March 10, 2023

Matlab QR

 

 

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’.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

A = Q x R

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:

Matlab QR 1

Output:

(Matrix X):

to compute the upper triangular matrix of a 4 x 5 matrix

(Upper Triangular Matrix):

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:

Matlab QR 4

Output:

(Matrix X):

Matrix X

(Upper Triangular Matrix):

Matlab QR 6

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:

orthogonal matrix of a 5 x 5 matrix

Output:

(Matrix Q):

Matlab QR 8

(Matrix R):

Matlab QR 9

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:

orthogonal matrix of a 4 x 4 matrix

Output:

(Matrix Q):

Matlab QR 11

(Matrix R):

Matlab QR 12

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 –

  1. What is Matlab?
  2. Matlab limit
  3. Plot graph Matlab
  4. Absolute Value Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW