%% This function is used to generate the error rates for KNN. 
%% K is the number of neighbors to consider, D is the distance between a 
%% sample and K neighbors, IDX are the indices of the K neighbors (in 
%% regards to the feature vector), and labels_train and labels_devtest are 
%% the labels for each sample from the training and test sets.

function [error_val conf_matrix_val] = findBestK(D, IDX, labels_train, labels_devtest)
    error_val = zeros(length(D{1}), 1);
    conf_matrix_val = cell(length(D{1}), 1);
    for i = 1:length(D{1})
         predicted_labels = findNearestNeighbor(i, D, IDX, labels_train);
         [acc_val conf_matrix_val{i}] = accuracy(predicted_labels,labels_devtest,unique(labels_train));
         error_val(i) = 1 - acc_val;
    end
end