function A_kdcca = KDCCA(CK_multiview,Label_multiview,num_class,num_train)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CK_multiview: multiview train centered kenel matrces, each view
% corresponds to one cell;
% Label_multiview: class label of X_multiview, type: string;
% each view has the same number of classes;
% num_class: class number
% num_train: the number of training samples in each class
% A_kdcca: multiple view-specific transforms, one for each view;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nV = length(CK_multiview);

N = num_class * num_train;
total_N = nV * N;

Ai = zeros(N,N);% DCCA: A = Ai*Ai'
for c = 1:num_class
    Ai(num_train*(c-1)+1:num_train*c,num_train*(c-1)+1) = 1;
end
A = Ai*Ai';

sA = zeros(total_N,total_N);
sB = zeros(total_N,total_N);

for r = 1:nV
    for c = r:nV
        rs = N*(r-1)+1;
        re = N*r;
        cs = N*(c-1)+1;
        ce = N*c;
        if r == c
            sB(rs:re,cs:ce) = CK_multiview{r,1} * CK_multiview{c,1}+0.01*eye(N);%nonsingularity  
        else
            sA(rs:re,cs:ce) = CK_multiview{r,1} * A * CK_multiview{c,1};
            sA(cs:ce,rs:re) = CK_multiview{c,1} * A * CK_multiview{r,1};
        end
    end
end
sB = (sB + sB')/2;
sA = (sA + sA')/2;
%% get a_kdcca

L = num_class-1;
%L = K;
% initiate a1,D{i,1}
[V E] = eigs(sA,sB,L);
for i =1:nV
     A_kdcca{i,1} = V(N*(i-1)+1:N*i,:); 
end
fprintf('DCCA finished\n');

