2

Consider the following minimal example:

function CoderEigFail() %#codegen
A = [0 sqrt(2); sqrt(2) 0];
[B C] = eig(A)

When I compile this via codegen CoderEigFail.m and run it, I get a different matrix B than I get when I run the original Matlab file instead. In particular, for the mex file I get B = [1 1; -1 1], while for the Matlab file I get B = [-0.7071 0.7071; 0.7071 0.7071]. I have Matlab R2011a running on MacOSx Mountain Lion.

Can anyone reproduce/explain this behavior? Is this a bug or am I missing something here?

TriSSSe
  • 264
  • 1
  • 7
  • 5
    Interesting "bug". Understand however that an eigenvector is just that, a vector. So essentially just a direction which can be arbitrarily scaled and still be valid (in the sense that the direction <1,2> is identical to the direction <2,4> and so on. Eigenvectors are only unique to within a scaling factor. In my experience Matlab always chooses to scale these eigenvectors (the columns of B) so that their 2-norm is unity. Apparently the compiled version either doesn't scale or it scales differently. Hopefully someone else can fill out more details on why the differences in scaling. – Stuart Apr 17 '13 at 16:57

1 Answers1

5

No it's not a bug.

As Stuart pointed out in the comment to the question, eigenvectors are generally normalized. This is pointed out in a note in the documentation of eig().

The difference in behavior with the compiled version is a documented feature in "Expected Differences in Behavior After Compiling MATLAB Code" under "For certain advanced library functions".

This behavior is simply acknowledged but not justified or explained.

Oleg
  • 10,406
  • 3
  • 29
  • 57