QR分解,matlab

时间:2017-11-19 10:46:27

标签: matlab linear-algebra

%% Gram—Schmidt as Triangular Orthogonalization
clear

M = [1,1,1; 1,1,0; 1,1,9]

[m,n] = size(M);
Q = M;
Rinv = eye(n);
for i = 1:n
    Ri = eye(n);
    Ri(i,i) = 1 / norm(Q(:,i));
    for j = i+1:n
        Ri(i,j) = -Q(:,i)'*Q(:,j) / norm(Q(:,i));
    end
Q = Q*Ri;
Rinv = Rinv*Ri;
end
Q
R = inv(Rinv)

问题:Q*R给出了M,但Q不是正对角线。

1 个答案:

答案 0 :(得分:0)

这应该返回正确的结果:

clear();

M = [1,1,1; 1,1,0; 1,1,9];
[m,n] = size(M);

Q = zeros(m,n);
R = zeros(n,n);

for j = 1:n
    v = M(:,j);

    for i = 1:j-1
        R(i,j) = Q(:,i).' * M(:,j);
        v = v - (R(i,j) * Q(:,i));  
    end

    R(j,j) = norm(v);
    Q(:,j) = v / R(j,j);
end

或者,您可以尝试我在Matlab文件交换中找到的以下实现(它包含许多已知的算法变体):https://it.mathworks.com/matlabcentral/fileexchange/55881-gram-schmidt-orthogonalization