plot a confusion matrix in matlab

To plot a confusion matrix in MATLAB, you need first to generate the confusion matrix. The confusion matrix is a matrix that represents the performance of a classification algorithm. It tells you how many observations were classified correctly and incorrectly.

Once you have the confusion matrix, you can use the MATLAB function confusionchart to plot the matrix. Here is an example code that generates a confusion matrix and plots it using the confusionchart function:

main.m
% Generate confusion matrix
actual = [3; 3; 2; 2; 1; 1]; % Actual labels
predicted = [3; 2; 2; 2; 1; 3]; % Predicted labels
C = confusionmat(actual,predicted) % Generate confusion matrix

% Plot confusion matrix
figure
confusionchart(C)
237 chars
9 lines

This will produce a plot of the confusion matrix that looks like this:

Confusion Matrix Plot

Note that the actual and predicted vectors used to generate the confusion matrix in this example represent three classes (labeled 1,2, and 3), but you can use this code with any number of classes.

gistlibby LogSnag