MATLAB: More non-zero generalized eigenvalues than in theory

eiggeneralized eigenvalues

Dear all,
I'm trying to compute generalized eigenvalues. First I've tried:
[V,D]=eig(A,B);
Other attempts
[V,D]=eig(A,B,'chol');
[V,D]=eig(A,B,'qz');
[V,D]=eig(A/B);
Well, my problem is that in all cases I obtain more non-zero eigenvalues than in theory. A,B are both of 42×42 size and rank(A)=2, rank(B)=41. (By the way, in case it's useful: A and B are between- and within-scatter matrices from a k=3 multiclass Linear Discriminant Analysis, i.e. three classes/groups).
Theoretically, since rank(A)=2, I would get two non-zero eigenvalues… but results are: [3.1055, 0.9127, 0.7718, etc.] All other are indeed <1e-12. I've also tried computing the scatter matrices of my data having been first transformed to zero-mean unit-stdev, but yet I get three non-negligible eigenvalues!
I don't know where the problem is. I've verified my code (mainly to check if my computation of matrices is correct) against a given example of size 6×6, and I do get two non-zero eigenvalues as expected.
Thank you very much in advance! Best regards,
Fernando

Best Answer

I think I've found the solution for this issue by using the intermediate output of the QZ algorithm.
[AA,BB,Q,Z,V,W] = qz(A,B,'real'); % in my case I think 'real' is safe, still needs a check
Right generalized eigenvectors are rows of W transposed. Eigenvales are:
D(i,i) = AA(i,i)/BB(i,i);
I guess that the code in eig() doesn't check if BB(i,i) has an aprox. 0 value, and numerical precision due to a 0/0 made an extra non-zero eigenvalue appear in my example data.
Let's see if I'm wrong or if anyone has a different explanation. Hope this helps!
Related Question