This is a matlab built in function called knnclassify, which is primarily used to identify the nearest neighbour of a data in matrix.
www.youtube.com