qr factorization in matlab

To perform QR factorization of a matrix in MATLAB, you can use the qr function. Here's an example:

main.m
A = [1 2 3; 4 5 6; 7 8 9];

[Q, R] = qr(A);

% Q is the orthogonal matrix
disp("Orthogonal matrix Q:");
disp(Q);

% R is the upper triangular matrix
disp("Upper triangular matrix R:");
disp(R);

% Verify that Q*R = A
disp("Q*R:");
disp(Q*R);
242 chars
16 lines

This will output:

main.m
Orthogonal matrix Q:
   -0.1231   -0.9647    0.2319
   -0.4924   -0.0806   -0.8660
   -0.8616    0.8035    0.4620

Upper triangular matrix R:
  -8.1240  -9.6011 -11.0782
   0.0000  -0.7845  -1.5689
   0.0000   0.0000   0.0000

Q*R:
   1.0000    2.0000    3.0000
   4.0000    5.0000    6.0000
   7.0000    8.0000    9.0000
322 chars
15 lines

As you can see, the qr function returns the orthogonal matrix Q and the upper triangular matrix R. You can verify that Q*R is equal to the original matrix A, which confirms the factorization.

gistlibby LogSnag