I’ve reduced the nearest neighbor method to 10 lines of code in Octave / Matlab:
function nearest_neighbors = NN_fully_vectorized(dataset, N)
dataset = dataset(:,1:N); %removes the hidden classifier
num_rows = size(dataset,1);
num_cols = size(dataset,2);
temp_matrix = repmat(dataset’, [1 1 num_rows]);
ref_dataset = shiftdim(temp_matrix,2);
diff_matrix = sum((dataset.-ref_dataset).^2,2);
zero_indeces = 1:num_rows+1:num_rows*num_rows;
diff_matrix(zero_indeces) = Inf; %sets the zero entries to infinity
[a b] = min(diff_matrix);
nearest_neighbors = reshape(b,[num_rows 1]);endfunction