-
Notifications
You must be signed in to change notification settings - Fork 3
/
trainCCA.m
34 lines (29 loc) · 1.05 KB
/
trainCCA.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [ Wx, Wy, eigv ] = trainCCA(Cxx, Cyy, Cxy, nDim)
% [Wx, Wy, eigv] = trainCCA(Cxx, Cyy, Cxy, nDim)
% Computes the Canonical Correlation Analysis projections on two data
% matrices X and Y.
%
% Input:
% Cxx - covariance matrix of X-X
% Cyy - covariance matrix of Y-Y
% Cxy - covariance matrix of X-Y
% nDim - number of CCA dimensions
%
% Output:
% Wx - CCA projection matrix for modality X
% Wy - CCA projection matrix for modality Y
% eigv - corresponding eigenvalues for returned projections
option = struct('disp', 0);
A = Cxy/Cyy*Cxy'; B = Cxx;
% Use eig() instead of eigs() for stable performance
% [eigvectors, eigv] = eigs(A, B, nDim, 'lr', option);
% eigv = real(eigv); eigvectors = real(eigvectors);
% if (~exist('eigv', 'var') || sum(diag(eigv)==0)~=0 || min(diag(eigv))<=0)
% disp('Use eig() instead');
[eigvectors, eigv] = eig(A, B);
[SD, SI] = sort(diag(eigv), 'descend');
eigv = diag(SD(1:nDim));
eigvectors = eigvectors(:, SI(1:nDim));
% end
Wx = eigvectors;
Wy = Cyy\Cxy'*eigvectors/sqrt(eigv);