%% Testing MNIST
% images = loadMNISTImages('./mnist_data/train-images.idx3-ubyte')';   % 60000x784 double array
% labels = loadMNISTLabels('./mnist_data/train-labels.idx1-ubyte');   % 60000x1 double array
% 
% figure;
% subplot(1, 3, 1);
% imshow(reshape(images(1, :), 28, 28));
% title(num2str(labels(1)));
% subplot(1, 3, 2);
% imshow(reshape(images(2, :), 28, 28));
% title(num2str(labels(2)));
% subplot(1, 3, 3);
% imshow(reshape(images(3, :), 28, 28));
% title(num2str(labels(3)));

%% Do job
train_images = loadMNISTImages('./mnist_data/train-images.idx3-ubyte')';
train_labels = loadMNISTLabels('./mnist_data/train-labels.idx1-ubyte');
test_images = loadMNISTImages('./mnist_data/t10k-images.idx3-ubyte')';
test_labels = loadMNISTLabels('./mnist_data/t10k-labels.idx1-ubyte');

C = cValue(train_images(1:60:end,:), train_labels(1:60:end))

svm_options = ['-s 0 -t 0 -q -h 1 -b 1 -c ', num2str(10^C)];
final_model = svmtrain(train_labels(1:60:end), train_images(1:60:end,:), svm_options);

svm_options2 = ['-b 1 -q'];
[pred_lab, acc, dec_val] = svmpredict(test_labels, test_images, final_model, svm_options2);
fprintf('The accuracy of the final model on the test set is %6.3f\n', acc(1));