# How to write MATLAb code to generate confusion matrices and calcultes recall and precision?

16 views (last 30 days)
Edited: MHN on 5 Feb 2016
Hi,
I've a data file of 101 records with 21 classes. First of all, I want to generate 21 separate confusion matrices for these 21 classes and then want to calculate recall and precision for these 21 confusion matrices. Please guide me that how can I write MATLAB code for this task?
Thank you.

MHN on 5 Feb 2016
Edited: MHN on 5 Feb 2016
You do not have to make 21 separate confusion matrices. You should just make one confusion matrix. E.g. let Y be a vector with 12 elements that shows the real classes of your instances. and let Y_hat be the predicted class of the instances. Then you can easily compute the confusion matrix by the following code:
Y = [1 1 1 1 2 2 2 2 3 3 3 3];
Y_hat = [1 1 1 3 2 3 1 1 3 3 3 3];
C = confusionmat(Y,Y_hat)
C is the confusion matrix.
The same for 101 instances and 21 classes. e.g (I have used a random vector as a real classes and then randomly changed 20 of them to make Y_hat which could be the result of a prediction):
Y = randi(21,101,1);
Y_hat = Y;
Y_hat(randi(101,20,1)) = randi(21,20,1);
[c,order] = confusionmat(Y,Y_hat);

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!