简体   繁体   English

Matlab中的qr分解算法中的错误

[英]error in qr factorization algorithm in matlab

I am supposed to implement the QR factorization using Matlab, and then test it on the matrix A = [1 2; 我应该使用Matlab实现QR分解,然后在矩阵A = [1 2; 1 1; 1 1; 2 3; 2 3; 2 2]. 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. 您的for循环从2:n运行,但尚未定义n。 You simply need to define n with: 您只需要使用以下命令定义n:

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并得到

>> 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). 请注意,以这种方式定义m,n可以使您泛化使用任何矩阵A (假设m> = n)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM