Question

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?

Was it helpful?

Solution

In a QR factorization as A=QR, R is an upper-triangular matrix. If your matrix A is invertible, then the factorization is unique if you impose the condition that R has real, non-negative diagonals. If this condition is relaxed, you get an orthogonal matrix Q and an upper-triangular matrix R, 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:

Compatibility Considerations: Since the QR factorization is not unique, these different results are still correct. Ensure that your code does not depend on the values of the elements of the factors Q and R.

Is this what's happening with your code? Does it depend on the form of Q and R?

As a quick fix, could you try this from R2010b:

which qr.m

On my machine, I get /Applications/MATLAB_R2010b.app/toolbox/matlab/matfun/qr.m. Could you try copying this function and putting it in your R2011 path, and rename it as qr2010 or something? Then whenever you need the old function, you can call qr2010 from 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 R2011a on my machine and checked out qr. 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.

A=magic(5);x=(1:5)';   %'
[Q R]=qr(A);
y=Q'*x;                %'
z=Q*y;

z'

ans =

    1.0000    2.0000    3.0000    4.0000    5.0000

I get the same result in both R2011a and R2010b. 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 qr to return the same matrices as in R2010b, then you should use morispaa's suggestion.

EDIT 2

Explanation of morispaa's solution: Since in 2010b, the diagonals of R are positive to get the same behavior in 2011a, all you need to do is get the sign of the diagonals and propagate it throughout the R matrix. For my example above, R is

R =

  -32.4808  -26.6311  -21.3973  -23.7063  -25.8615
         0   19.8943   12.3234    1.9439    4.0856
         0         0  -24.3985  -11.6316   -3.7415
         0         0         0  -20.0982   -9.9739
         0         0         0         0  -16.0005

and D is

D =

    -1     0     0     0     0
     0     1     0     0     0
     0     0    -1     0     0
     0     0     0    -1     0
     0     0     0     0    -1

The diagonals of R automatically become positive (it's as simple as -1*-1=1). Similarly, you propagate the sign in the Q matrix. Note that D*D is simply the square of the elements for a diagonal matrix, and is equal to I, the identity matrix. Hence, we get

 Q2*R2=Q*D*D*R
      =Q*I*R
      =Q*R
      =A

OTHER TIPS

I think (don't have MATLAB at hand at the moment) that you can restore the behavior of the version 2010b as follows:

[Q R] = qr(Z);
D = diag(sign(diag(R)));
R2 = D*R;
Q2 = Q*D;

Now R2 will be upper triagonal, have positive diagonal, Q2 stays orthogonal (or unitary) and Q2*R2 = Z. In complex case use

R2 = D'*R;
Q2 = Q*D;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top