Lets use the cifar10 demo included in Matlab for your question
clearvars
close all
clc
load('Cifar10Labels.mat','trueLabels','predictedLabels');
[m,order] = confusionmat(trueLabels,predictedLabels);
figure
cm = confusionchart(m,order);
Please look at the confusionchart and consider the following for one particular class c,
- The diagonal element of class c is the amount of TP
- Everything inside the predicted class column except the diagonal element is falsely predicted as class c --> FP
- Everything inside the true class row except the diagonal element is of class c but not predicted as c --> FN
- Every other diagonal element except the diagonal element of class c itself is TN.
Now, we can put this easily in in code by summing up row, column and diagonal elements and substracting the TP. Lets pick a class, e.g. c=2 "automobile", and calculate TP, FP, FN, TN for that class
c = 2;
TP = cm.NormalizedValues(c,c)
FP = sum(cm.NormalizedValues(:,c))-TP
FN = sum(cm.NormalizedValues(c,:))-TP
TN = sum(diag(cm.NormalizedValues))-TP