how to calculate the qr decomposition of a matrix in matlab

To calculate the QR decomposition of a matrix in Matlab, you can use the qr() function. The function takes a single input argument, the matrix to be decomposed, and returns two matrices, Q and R, such that A = QR.

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

% calculate QR decomposition
[Q, R] = qr(A);

% display results
disp('Q = ');
disp(Q);
disp('R = ');
disp(R);
153 chars
12 lines

This will output:

main.m
Q = 
   -0.12309   0.90453  -0.40825
   -0.49237   0.30151   0.81650
   -0.86165  -0.30151  -0.40825
R = 
  -8.12404  -9.60114  -9.12402
   0.00000  -0.90453  -1.80891
   0.00000   0.00000   0.40825
199 chars
9 lines

In the above example, Q and R are the decomposition of the input matrix A. The Q matrix is an orthogonal matrix, and the R matrix is an upper triangular matrix.

gistlibby LogSnag