Home > Net >  Matlab and Eigen eigenvectors differ only by the sign
Matlab and Eigen eigenvectors differ only by the sign

Time:10-11

I am working with a project which consists in translating Matlab code to C/C . At one point I have to calculate the eigenvectors of a matrix, in Matlab this is done using the eig function while in C I use EigenSolver from the Eigen library.

The problem is that some eigenvectors (apparently random) have the opposite sign compared with the equivalent Matlab one and this could be a problem in my application.

I know that eigenvectors are not unique, so is there any method to know which eigenvectors will have the sign changed and correct it simply multiplying by -1?

CodePudding user response:

According to the documentation of eig, the sign of the eigenvectors is not guaranteed to be consistent between MATLAB releases and/or machines used:

Different machines and releases of MATLAB® can produce different eigenvectors that are still numerically accurate:

  • For real eigenvectors, the sign of the eigenvectors can change.
  • For complex eigenvectors, the eigenvectors can be multiplied by any complex number of magnitude 1.

Thus replicating what MATLAB does is not possible, because it is not defined.

However, if you want to modify both the MATLAB code and the C code, then you can force the eigenvectors to be in a specific hemicircle/hemisphere, for example by requiring that the first component be positive (multiply the vector by -1 if the x-component is negative). I think this should be sufficient to get consistent results.

  • Related