How to calculate Precision and Recall using confusion matrix in Matlab? I am working on 3 class problem.How to calculate precision,recall ,f-score,MCC of each class while using  MATLAB. Here is my confusion matrix:

2775  0    0
1    591   0
4     0   845


I am calculating Accuracy from Confusion matrix in this way:

Accuracyy = 100*sum(diag(confusionMat))./sum(confusionMat(:));  


I want to measure below performance measures for each class.I know the formulas but how to execute this in MATLAB. Please help!!! I would really appreciate! Thank you!


Precision=TP / (TP + FP) 
Recall= TP / (TP + FN) 
F-score = 2*TP /(2*TP+ FP + FN) 
Matthews Correlation Coefficient (MCC)
if yHat are your predictions and yval are your y true then

tp = sum((yHaT == 1) &amp; (yval == 1));
fp = sum((yHaT == 1) &amp; (yval == 0));
fn = sum((yHaT == 0) &amp; (yval == 1));

precision = tp / (tp + fp);
recall = tp / (tp + fn);
F1 = (2 * precision * recall) / (precision + recall);