This is just a brute force implementation of k nearest neighbor search without using any fancy data structure, such as kd-tree. However it is the fastest knn matlab implementation I can find.
A partial sort mex function is implemented which is a simple wrapper of c++ partial_sort.
Provided the sort function, the matlab code is only of two lines. However, it is extremely fast.
[distance, index] = knn(query,data,k);