I am supposed to implement the QR factorization using Matlab, and then test it on the matrix A = [1 2; 1 1; 2 3; 2 2]. This is the code I tried to write until now. But I am getting some errors and I'm not sure where the problem is. Can anyone spot it?
A = [1 2; 1 1; 2 3; 2 2];
m = 4
Q=A;
Q(:,1) = A(:,1)/norm(A(:,1));
K = eye(m);
for j=2:n
Q(:,j) = ((K - Q(:,j-1)*Q(:,j-1)')*A(:,j))/norm((K - Q(:,j-1)*Q(:,j-1)')*A(:,j));
K = K - Q(:,j-1)*Q(:,j-1)';
end
R=Q'*A;
Your for loop runs from 2:n
, but you haven't defined n. You simply need to define n with:
A = [1 2; 1 1; 2 3; 2 2];
[m,n] = size(A);
When I make this addition, I can evaluate Q*R and get
>> Q*R
ans =
1.0000 2.0000
1.0000 1.0000
2.0000 3.0000
2.0000 2.0000
Note that defining m,n
this way lets you generalize to use any matrix A
(assuming m>=n).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.