EDIT:
Thank you @yoda and @morispaa. You are both right and @morispaa’s solution works, i.e. my processing of the transformed coefficients, which is based on assumptions about the space spanned by Z, and the order and "orientation" of the Z vectors, renders the correct result if I update the sign of the columns of Q so that the diagonal of R has positive elements.
For more details about the transformation I am working on you can read this; Z below = sampled Zernike polynomials, which are known for not being orthogonal nor complete on the discrete case (our case).
Intuition on why the solution proposed by @morispaa works. I would love to hear your input about it:
My intuition is that somehow enforcing real non-negative diagonals in R renders a basis Q that "aligns" better with the vectors in Z (which, as I said earlier, is non-unitary), and therefore the Options 1 and 2 below, even though they represent different transformations, output coefficients probably in a similar space.
More specifically, I think Z is "almost" unitary, and maybe this leads the QR decomposition to return a basis that is close enough to Z? Only then I can imagine that my processing of the transformed coefficients, which is based on assumptions about the specifics of the vectors in Z, work when the diagonal of Q is fully positive, but not when it has negative entries. What do you think?
Background
I have both MATLAB R2011a and R2010b installed on my machine.
One of the changes from R2010b to R2011a affects the implementation of qr() (see the release notes about this particular change here).
An important part of one my projects uses qr() to estimate an orthogonal basis for a direct and inverse transformation. My code applies this transformation to an input signal, processes the transformed coefficients and returns back the processed signal. In other words, the changes made in R2011a to qr() made the block that process the coefficients of this transformation to stop working (the inverse transformation does not return the expected inverse transformation of the processed signal).
Somehow the Q matrix that is now returned from qr() is different from the older version in a way that prevents the processing of the transformed coefficients from working properly.
First question
In light of the above, is it possible to tell R2011a to use qr() from R2010b?
Second question
I use Q and Q’ to compute the direct and inverse transformation; you can see more details here. More specifically, I use y = Q * x and x = Q’ * y to compute the direct and inverse transformation respectively. A different way to compute the direct transform is using least squares. In other words we have two options:
Option 1: Direct and inverse transform using QR factorization:
% Direct:
[Q R] = qr(Z);
y = Q' * x;
% Some processing of the y coefficients
% ...
% Inverse:
x = Q*y;
Option 2: Direct and inverse transform via least squares fitting
% Direct:
y = Z \ x;
% Some processing of the y coefficients
% ...
% Inverse:
x = Z*y;
where our variables are:
% x = Input vector
% y = Direct transformation of x
% Z = Matrix with sampled basis
In R2011a the Option 1 above stopped working (it works in R2010b). I really like the idea of using qr() for the direct and inverse transform (it’s much faster than computing least-squares for every new vector). If I wanted to use the new qr() for my project, does anybody know how to make my transformation work again with the new Q?
In a QR factorization as
A=QR,Ris an upper-triangular matrix. If your matrixAis invertible, then the factorization is unique if you impose the condition thatRhas real, non-negative diagonals. If this condition is relaxed, you get an orthogonal matrixQand an upper-triangular matrixR, but they needn’t be unique.The non-uniqueness of decompositions often confuses people, and I thought I’d point you to one of my favorite examples: the eigenvectors of an identity matrix.
From the link you provided:
Is this what’s happening with your code? Does it depend on the form of
QandR?As a quick fix, could you try this from
R2010b:which qr.mOn my machine, I get
/Applications/MATLAB_R2010b.app/toolbox/matlab/matfun/qr.m. Could you try copying this function and putting it in yourR2011path, and rename it asqr2010or something? Then whenever you need the old function, you can callqr2010from the latest version of MATLAB and it should use the old algorithm. I haven’t tested this, so let me know if it works.EDIT
I installed
R2011aon my machine and checked outqr. Like I said, not enforcing the positive diagonals will result in different combinations for the signs of the elements, thereby rendering the solution non-unique. However, the forward and inverse transformations should work and it does so on my machine.I get the same result in both
R2011aandR2010b. So, my guess is somehow you’re relying on the diagonals being positive, which is probably not a good thing to do.But if you want to retain your code, but instead get the new
qrto return the same matrices as inR2010b, then you should use morispaa’s suggestion.EDIT 2
Explanation of morispaa’s solution: Since in
2010b, the diagonals ofRare positive to get the same behavior in2011a, all you need to do is get the sign of the diagonals and propagate it throughout theRmatrix. For my example above,Risand
DisThe diagonals of
Rautomatically become positive (it’s as simple as-1*-1=1). Similarly, you propagate the sign in theQmatrix. Note thatD*Dis simply the square of the elements for a diagonal matrix, and is equal toI, the identity matrix. Hence, we get