% Given Q1 and Q2 such that Q1'* Q1 + Q2'* Q2 = I, the
% C-S Decomposition is a joint factorization of the form
% Q1 = U1*C*V' and Q2=U2*S*V'
% where U1,U2,V are orthogonal matrices and C and S are diagonal
% matrices (not necessarily square) satisfying
% C'* C + S'* S = I
% The diagonal entries of C and S are nonnegative and the
% diagonal elements of C are in nondecreasing order.
% The matrix Q1 cannot have more columns than rows.
% ( Submitted by S. J. Leon )
function [u1,u2,v,c,s]=csd(q1,q2)
[m,n]=size(q1);[p,n]=size(q2);
[u1,c,v]=svd(q1);
z=eye(n);z=hankel(z(:,n));
c(1:n,:)=z*c(1:n,:)*z;u1(:,1:n)=u1(:,1:n)*z;v=v*z;
q2=q2*v;
k=1;
for j=2:n
if c(j,j)<=1/sqrt(2)
k=j;
end
end
b=q2(:,1:k);
[u2,r]=qr(b);
s=u2'*q2;
t=min(p,n);tt=min(m,p);
if k