function printConfusionMatrix(cf)
%%% printConfusionMatrix(confusionMatrix(CV.msf));

nTasks = size(cf,1);

fprintf(1,'                           Actual\n');
fprintf(1,'                 ');
for i=1:nTasks
  fprintf(1,'  Task %d',i);
end
fprintf(1,'\n');

for i=1:nTasks
  if i==round(nTasks/2)
    fprintf(1,'Predicted ');
  else
    fprintf(1,'          ');
  end
  fprintf(1,'Task %d',i);
  for j=1:nTasks
    fprintf(1,' %7.1f',cf(i,j));
  end
  fprintf(1,'\n');
end

fd = fopen('cftable.tex','w');

fprintf(fd,'\\begin{table}\n');
fprintf(fd,'\\begin{center}\n');
fprintf(fd,'\\begin{tabular}{cc');
for i=1:nTasks
  fprintf(fd,'r@{.}l');
end
fprintf(fd,'}\n');
midtask = round(nTasks/2);
printampersands(fd,2);
fprintf(fd,'   \\multicolumn{%d}{c}{Actual} \\\\\n',nTasks*2);
printampersands(fd,1);
fprintf(fd,'          ');
for i=1:nTasks
  fprintf(fd,'& \\multicolumn{2}{c}{Task %d} ',i);
end
fprintf(fd,'\\\\\n');
for pi = 1:nTasks
  if pi==midtask
    fprintf(fd,'Predicted & ');
  else
    fprintf(fd,'          & ');
  end
  fprintf(fd,'Task %d ',pi);
  for i=1:nTasks
    v = cf(pi,i);
    vi = floor(v);
    vd = round((v-vi)*10);
    fprintf(fd,'& ~~%d&%d ',vi,vd);
  end
  fprintf(fd,'\\\\\n');
end
fprintf(fd,'\\end{tabular}\n');
fprintf(fd,'\\end{center}\n');
fprintf(fd,'\\caption{Table caption.}\n');
fprintf(fd,'\\label{tab:label}\n');
fprintf(fd,'\\end{table}\n');

fclose(fd);

fprintf(1,'LaTeX table written to cftable.tex\n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function printampersands(fd,n)
for i=1:n
  fprintf(fd,'&');
end


