function [cf,allcf] = confusionMatrix(cv)
%  [cf,allcf] = confusionMatrix(cv)

nTestTrials = length(cv.testResults);
nTasks = size(cv.classes,1);

sumcf = 0;
for ttrial = 1:nTestTrials
  classes = cv.testResults{ttrial}{5};
  nPerTask = round(length(classes)/nTasks);
  if nPerTask*nTasks ~= length(classes)
    error('Not even number of samples per class');
  end
  classes = reshape(classes,nPerTask,nTasks);
  onecf = hist(classes,nTasks);  %true in cols, predicted in rows
  onecf = onecf ./ (repmat(sum(onecf),nTasks,1)) * 100;
  allcf{ttrial} = onecf;
  sumcf = sumcf + onecf;
end
cf = sumcf / nTestTrials;

%printConfusionMatrix(cf);
