%% leave-one-trial-out

%data: (ch*time*trial)
%excludeTrials: the trials index of input data i.e. template should not include these
%        trials

function [ out ] = myCCA3( data , excludeTrials)

numTrials = (1:size(data,3));
newData = squeeze(mean(data(:,:,excludeTrials),3))';
template = squeeze(mean(data(:,:,numTrials(~ismember(numTrials,numTrials(excludeTrials)))),3))';

if(isempty(numTrials(~ismember(numTrials,numTrials(excludeTrials)))))%
    [A,B,r,U,V] = canoncorr(newData,newData);
else
    [A,B,r,U,V] = canoncorr(newData,template);
end
out = r(1);

% numTrials = (1:size(data,3));
% newData = data(:,:,excludeTrials);
% template = data(:,:,numTrials(~ismember(numTrials,numTrials(excludeTrials))));
% [A,B,r,U,V] = canoncorr(newData,template);
% out = r(1);

end

