load('data_app');
xa = x; Sa = S;
load('data_test');

freqs = zeros(1, 10);
for i = 1 : 10
    freqs(i) = size(find(Sa == i), 1) / size(Sa, 1);
end;

figure();
hold on;
plot(freqs, '.');
xlim([1 10]);
ylim([0 1]);

for i = 1 : 10
    freqs(i) = size(find(S == i), 1) / size(S, 1);
end;

plot(freqs, '+');
hold off;

k = 1;

S_calc = zeros(size(S));
for i = 1 : size(x, 1)
    S_calc(i) = do_kppv(x(i,:), xa, Sa, k);
end;

diff = S_calc - S;
size(find(diff ~= 0), 1)

confusion = zeros(10, 10);

for i = 1 : 10
    for j = 1 : 10
        confusion(i, j) = size(find(S_calc(find(S == i)) == j), 1);
    end;
end;

confusion