Sign differences in QR decomposition

32 views (last 30 days)
I am trying to use a simple QR decomposition and compare the results to that of the qr matlab function. I generate a random A matrix and then compare the Q and R values separately. For some reason, although the magnitudes of each element is the same there is sometimes a difference in sign between some of the terms. This is fine in the fact that Q*R = A in both cases, however I need to use R*Q. Any help on what is happening/how to fix it?
m = 4;
casenum = randi(3,1)
if casenum == 1
A = randn(m)
end
if casenum == 2
A = i*randn(m)
end
if casenum == 3
pownum = randi(2,m);
A = randn(m) + randn(m).*(i.^pownum)
end
[Q1,R1] = qr(A);
Q2 = zeros(m,m);
R2 = zeros(m,m);
for i=1:m
% Grab the next column of A.
qbar = A(:,i);
% Compute the current vector projection onto the previous columns of Q
R2(:,i) = Q2'*qbar
% Subtract out the projections so qbar is orthogonal
qbar = qbar - Q2*R2(:,i);
% Normalize qbar and append it to Q
R2(i,i) = norm(qbar);
Q2(:,i) = qbar/R2(i,i);
end
  1 Comment
Jan
Jan on 2 Aug 2013
When Q*R=A is correct in both cases, both cases are correct results. This means unequivocally, that there is no "sign error" and I have deleted the corresponding tag. As Richard has explained already, the QR-decomposition is not unique. So you observe the expected effects.

Sign in to comment.

Accepted Answer

Richard Brown
Richard Brown on 1 Aug 2013
It's only unique up to the signs of the rows of R. If you want to enforce positive diagonals of R, and thereby get a unique factorisation, construct
D = diag(sign(diag(R)));
and then
Qunique = Q*D; Runique = D*R;

More Answers (0)

Categories

Find more on Matrix Decomposition in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!