clc;
clear all;

% Load dataset
x_train=LoadMNISTImages('train-images-idx3-ubyte');
x_test=LoadMNISTImages('t10k-images-idx3-ubyte');
y_train=LoadMNISTLabels('train-labels-idx1-ubyte');
y_test=LoadMNISTLabels('t10k-labels-idx1-ubyte');
 
% Matrix transpose
x_train=x_train';
x_test=x_test';
%% SVM
t = templateSVM('Standardize',true)
SVMMdl = fitcecoc(x_train,y_train,'Learners',t,...
    'ClassNames',{'0','1','2','3','4','5','6','7','8','9'});
predict_label = predict(SVMMdl,x_test);

s=0;
for i=1:length(predict_label)
    if str2double(predict_label(i))==y_test(i)
        s=s+1;
    end
end
acc=s/length(predict_label)
figure
plot(str2double(predict_label),'b*')
hold on;
plot(y_test,'ro')
hold on;
legend('predict_label','real_label')
title('SVM Model')
figure
confusionchart(str2double(predict_label)',y_test')
title('SVM Model Confusion matrix')