% Prints a confusion matrix, i.e., a table that shows the number of
% each kind of misclassification. On row i, column j, you have the
% number of times the classifier has guessed class j when in reality
% the item is in class i. A row will sum up to the number
% of real items in a class.
%
% An ideal classifier yields non-zero only on
% the diagonal. For non-trivial data such situation is miraculous (so
% first thing then is to check your test case for programming errors
% ;))
%
% Instead of just printing, you could return the matrix, and use the
% information to gain more knowledge about how your data behaves. For
% example, to build tree-like classifiers that first separate "easy"
% classes that never get mistaken (where both the row and column
% contain only the diagonal element) and you train further classifiers
% using only the "not-so-easy" classes as training data, hoping
% they'll be more easily separated without the other classes mixing
% things up.

function confusion_matrix(outputs, trueOutputs)
  outputs = class_compete(outputs);
  
  header = sprintf('_____');
  for j=1:size(outputs,1)
    header = sprintf('%s %3d', header, j);
  end
  disp(header);
  
  for i=1:size(outputs,1)
    pr = sprintf('%3d: ', i);
    for j=1:size(outputs,1)
      pr = sprintf('%s %3d', pr, sum(outputs(j,:)==1 & trueOutputs(i,:)==1));
    end
    disp(pr);
  end
  