%% Reorder QZ Factorization
% Compute the QZ factorization of a pair of matrices, and then reorder the factors according to a specified ordering of the eigenvalues.
%
% Find the QZ factorization, or generalized Schur decomposition, of a pair of matrices A and B. This decomposition results in the factors \textrm{AA}=\textrm{QAZ} and \textrm{BB}=\textrm{QBZ}.

A = magic(5);
B = hilb(5);
[AA,BB,Q,Z] = qz(A,B)

%
% Since AA and BB are triangular, use ordeig to extract the eigenvalues from the diagonal blocks of AA and BB.

e = ordeig(AA,BB)

%
% Separate the eigenvalues into clusters, with the real positive eigenvalues (e>0) forming the leading cluster. Reorder the matrices AA, BB, Q, and Z according to this ordering of the eigenvalues.

[AAS,BBS,QS,ZS] = ordqz(AA,BB,Q,Z,'rhp')

% Examine the new eigenvalue order.

E2 = ordeig(AAS,BBS)

% Copyright 2012 The MathWorks, Inc.