File Exchange

image thumbnail

Multi Class Confusion Matrix

version (7.76 KB) by Abbas Manthiri S
Calclating kappa, acuuracy,error,Sensitivity ,Specificity , Precision,False positive rate etc.


Updated 21 Jul 2017

View License

This code is designed for two or more classes instance confusion matrix formation and Calclating
3.Sensitivity (Recall or True positive rate)
6.FPR-False positive rate
8.MCC-Matthews correlation coefficient
9.kappa-Cohen's kappa
Run demo.m for proof and demo
Developer Er.Abbas Manthiri S
Date 25-12-2016
Mail Id:
Coding is based on attached reference

Cite As

Abbas Manthiri S (2020). Multi Class Confusion Matrix (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (35)

Please,Can you update the codec for multiclass .

Thank you Richa and Li Yew! You helped me a lot with the code corrections!

ali kazemi

Hi Abbas,
Thank you for the code. I ask you one question why AccuracyOfSingle equal with sensitivity in the result of Multi-Class??
This is a mistake, right?

Li Yew

Thank you for developing such a useful tool.

for those faces same issue with me...As my input data is in categorical format, so i change the num2str(class_list(i)) to char(class_list(i)), then everything is fine.


Hi, Can anyone please help to clarify what does the "Error" signifies here. Is it EER (Equal Error Rate)?


This is awesome, thanks Abbas

Wander full, Save lots of time to compute each stat, Thank you



To make this work for mutliclass in case Class List is not same .. i commented the error condition "error('Class List is not same in given inputs')" and "error('Class List in given inputs are different')". Also, tweaked the code on line 81 as
if length(un_actual) >= length(un_predict)


I get error as "Class List is not same in given inputs" while running it on my data. Please help



When i call classify() it returns the Predicted_labels in "categorical" format, whivh while passing it to geMatrix gives an error that it does not support catgorical values. How to resolve this


i got the results for Precision and recall but the graph i am plotting is not giving the desired plot what to do now.?? what mistake i am making anyone please help.

Sabri Kr


This is a useful submission. It would be great if the class name was different, as it overloads the MATLAB confusion function:

Thanks Abbas, great code, saved me quite some time!

I update the file
Thanks for feedback

@this line
i put mistake please change

i will update that code
on sunday
thank for giving feed back

Thank you abbas for making corrections.Excellent contribution from your side.
If you want to improve it more.Make this code capable for dealing with multi-class with binary (0,1) values only
having multiple rows/col.Just like this example. code gives error on this example dataset.This code only deals with single rows and single col as input/output.

@Machine Learning Enthusiast

File updated
Thanks for feed back

@Machine Learning Enthusiast
I Checked my coding for you
i calculate the single class accuracy rate so change the formula
to in line 192
And formula available only to calculate Accuracy for two class only
we dont have formula for single class accuracy over multiclass

thanks for your feedback

Secondly can you please check.It calculates the True Negative correctly?.In my case it calculates wrong

excellent code.I am dealing with 3 class problem.I have just one question in codeline 192, when calculating the accuracy for each class. It gives the wrong accuracy for each class?.Any insight on this from your side will be greatly appreciated.

@Carlos Lopez Vydrin because multi-class confusion matrix changes formula compare to two class confusion matrix

Thank you for this wonderful code. I have just one question in codeline 192, when calculating the accuracy for each class. Why have you put an "Case/otherwise"-statement and why is the "accuracy"-calculations different in codeline 192 and 187?

Many thanks

@ Kh. Islam Thank you

Kh. Islam

Thank you for your efforts.


small typo changed
error now give correct value

Bugs removed
Added sub Accuracy for each by in total and alone


nothing changed

code improved

Name changed

publish view improved
to view click on demo

Coding converted to class method

Clear Publish view

html view

bugs removed

extra datum given to output

bugs removed

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

confusion matrix/

confusion matrix/html/