Thursday, August 24, 2006

CCA matlab code

The following code performs CCA analysis. Inputs are the data matricies X and Y, with one data point per row. The output is the directions of maximum correlation wx and wy, i.e., those values that maximize the value (X*wx)'*(Y*wy)

%Start CCA:


%calculate covariance matricies
Sxx = X'*X ;
Syy = Y'*Y ;
Syx = Y'*X ;
Sxy = X'*Y ;

[r c] = size( Sxy );

A = [ zeros(r,c) Sxy; ...
Syx zeros(r,c) ];

B = [ Sxx zeros(r,c); ...
zeros(r,c) Syy];

%solve the generalized eigenvalue problem: A*w = c*B*w
[V D] = eig(A, B);
%[V D] = eig( inv(B)*A ); %alternative way of solving problem

%find the directions of max correlation
[maxVal maxInd] = max( diag(D,0) );

w = V(:,maxInd); %Get collumn coresponding to max eigenvalue
wx = w(1:2);
wy = w(3:4);

1 Comments:

At 3:29 AM, Blogger Anand Kumar said...

Whether it is for Sparse matrixes...

 

Post a Comment

<< Home