% this function tries all the k valules from 1 to size of the dataset
% and then it returns the best k value for -let us say- 10 instances

function [bestKValue] = bestFloatingK(selectedRow,train)

% below array keeps the best k values and related mres
bestKValue = 1;
bestMre = 1e9;

    
    % initially let the best Mre value be 1
    [tempEffort, bestMre] = nnk(selectedRow, train, 1);
    
    % now try out other k values
    for j = 2:size(train,1)
        
        [tempEffort, tempMre] = nnk(selectedRow, train, j);
        % if the new mre is better, then update best k value
        if tempMre < bestMre
           bestMre = tempMre;
           bestKValue = j;
        end
        
    end
 

end