%% Calculate Confusion Matrix
% Load a sample of predicted and true labels for a classification problem. trueLabels are the true labels for an image classification problem and predictedLabels are the predictions of a convolutional neural network.

load('Cifar10Labels.mat','trueLabels','predictedLabels');

% Calculate the numeric confusion matrix. order is the order of the classes in the confusion matrix.

[m,order] = confusionmat(trueLabels,predictedLabels)

%
% You can use confusionchart to plot a the confusion matrix as a confusion matrix chart.

figure
cm = confusionchart(m,order);

% You do not need to calculate the confusion matrix first and then plot it. Instead, plot a confusion matrix chart directly from the true and predicted labels. You can also add column and row summaries and a title.

figure
cm = confusionchart(trueLabels,predictedLabels, ...
    'Title','My Title', ...
    'RowSummary','row-normalized', ...
    'ColumnSummary','column-normalized');

%
% The ConfusionMatrixChart object stores the numeric confusion matrix in the NormalizedValues property and classes in the ClassLabels property.

cm.NormalizedValues
cm.ClassLabels

%
% Copyright 2018 The MathWorks, Inc.