function cm = confusionMatrixFromIndices(test_outputs, true_outputs, ...
					 nclasses);
% Produces a confusion matrix.
%
% Inputs:
%
%   test_outputs    - classification output of the MLP being verified;
%                     coded as class indices.
%
%   true_outputs    - known true output that the guessed output is to be
%                     compared with; coded as class indices.
%
%   nclasses        - (optional) You may force the number of
%                     classes for weird cases when some classes
%                     were not included in the test set. Otherwise
%                     we deduce number of classes = maximum class
%                     index found among the vectors.
%
% Output:
%
%   cm - the confusion matrix; on the i:th row, j:th column, you have the
%        number of items predicted in class i when the true class is j.
%        Each column will thus sum up to the number of true items in a class.
%

if nargin==2
  nclasses = max([true_outputs(:) ; test_outputs(:)]);
end

cm = zeros(nclasses,nclasses);
for ic = 1:nclasses
  for jc = 1:nclasses
    cm(ic,jc) = sum(test_outputs==ic & true_outputs==jc);
  end  
end
