MATLAB: starting a function from a previous version

EDIT:

Thanks @yoda and @morispaa. You are right, and the @morispaa solution works, that is, my processing of converted coefficients based on the assumptions about space stretched over Z and the order and "orientation" of the vectors Z gives the correct result if I update the sign of the columns of Q so that the diagonal R has positive elements.

For more information on the conversion I'm working on, see this ; Z below = sampled Zernike polynomials, known as not , orthogonal or complete in the discrete case (our case).

Intuition about why the solution proposed by @morispaa works. I would like to hear your input on this:

My intuition is that somehow the implementation of real non-negative diagonals in R gives rise to Q , which aligns β€œbetter with vectors in Z (which, as I said earlier, is not unitary), and therefore options 1 and 2 below, even if they represent various transformations, the output coefficients are likely to be in a similar space.

In particular, I think that Z is "almost" unitary, and perhaps this leads to a QR decomposition to return a basis close enough to Z ? Only then can I imagine that my processing of the converted coefficients, based on the assumptions about the specifications of the vectors in Z , works when the diagonal Q is completely 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 for this specific change here ).

An important part of one of my projects uses qr() to evaluate the orthogonal basis for the forward and reverse transforms. My code applies this conversion to the input signal, processes the converted coefficients, and returns the processed signal. In other words, the changes made in R2011a to qr() made the block that processes the coefficients of this transform stop working (the inverse transform does not return the expected inverse transform of the processed signal).

Somehow, the matrix Q , which is now returned from qr() , differs from the old version in such a way that prevents the correct processing of the converted coefficients.

First question

In light of the above, can you tell R2011a to use qr() from R2010b ?

Second question

I use Q and Q ' to compute the forward and reverse transforms; You can see more details here . More specifically, I use y = Q * x and x = Q '* y to calculate the direct and inverse transforms, respectively. Another way to calculate the direct transform is to use the least squares. In other words, we have two options:

Option 1: Forward and inverse using QR factorization:

 % Direct: [QR] = qr(Z); y = Q' * x; % Some processing of the y coefficients % ... % Inverse: x = Q*y; 

Option 2: Forward and Inverse Least Squares Conversion

 % Direct: y = Z \ x; % Some processing of the y coefficients % ... % Inverse: x = Z*y; 

where are our variables:

 % x = Input vector % y = Direct transformation of x % Z = Matrix with sampled basis 

In R2011a, option 1 above stops working (it works in R2010b ). I really like the idea of ​​using qr() for direct and inverse transforms (this is much faster than calculating the least squares for each new vector). If I wanted to use the new qr() for my project, does anyone know how to make my transformation again with the new Q ?

+4
source share
2 answers

In factoring QR as A=QR , R is the upper triangular matrix. If your matrix A is invertible, then factorization is unique if you impose the condition that R has real non-negative diagonals. If this condition is relaxed, you get the orthogonal matrix Q and the upper triangular matrix R , but they need not be unique.

The nonuniqueness of decompositions often confuses people, and I thought I would point you to one of my favorite examples: eigenvectors of the identification matrix .

From the link you provided:

Compatibility: Since QR factorization is not unique, these different results are still true. Make sure your code does not depend on the values ​​of the elements of factors Q and R.

Is this what happens to your code? Does it depend on the form of Q and R ?

As a quick fix, you can 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 renaming it as qr2010 or something else? Then, when you need the old function, you can call qr2010 from the latest version of MATLAB and use the old algorithm. I have not tested this, so let me know if this works.

EDIT

I installed R2011a on my machine and checked qr . As I said, not fulfilling positive diagonals will lead to various combinations of signs of signs, thereby making the decision unique. However, forward and reverse conversions should work, and this is done on my machine.

 A=magic(5);x=(1:5)'; %' [QR]=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, I assume that you rely on diagonals that are positive, which is probably not very good.

But if you want to save your code, but instead get a new qr to return the same matrices as in R2010b , then you should use the morispaa clause.

EDIT 2

Explanation of the solution morispaa: since the diagonals from R positive in 2010b , in order to get the same behavior in 2011a , you only need to get the sign of the diagonals and extend it along R 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 

Diagonals R automatically become positive (this is as simple as -1*-1=1 ). Similarly, you propagate the sign in the matrix Q Note that D*D is just the square of the elements for the diagonal matrix and is equal to the I identity matrix. Therefore, we obtain

  Q2*R2=Q*D*D*R =Q*I*R =Q*R =A 
+2
source

I think (at the moment I do not have MATLAB) that you can restore the behavior of version 2010b as follows:

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

Now R2 will be upper triangular, has a positive diagonal, Q2 remains orthogonal (or unitary), and Q2*R2 = Z In a difficult case

 R2 = D'*R; Q2 = Q*D; 
+2
source

All Articles