% accuracy.m % % computes the confusion matrix and average misclassification error (also called accuracy) % conf_matrix is a matrix with element conf_matrix(i,j) showing % how many examples from class 'i' were predicted to be of class 'j' % Note that it works on multiclass problems. function [acc, conf_matrix] = accuracy(pred_y,true_y) u = unique(pred_y); for i=1:length(u) for j=1:length(u) conf_matrix(i,j) = length(find(pred_y == u(j) & true_y == u(i))); end end acc=sum(diag(conf_matrix))/length(true_y); return