function bestSubject = knnFace(facePath, k, dctData, dctDim, dctDataClass) %{ facefile path to the face image file k the number of close neighbors to find dctData the pretrained dataset dctDim the dimesion of DCT data to work with %} % Find the number of faces in the dataset N = length(dctData); % Get the 1-D feature vector of the DCT of the face features = findfeatures(facePath, dctDim); % Find the distance from the face to each face in the dataset dists = zeros(N, 1); for idx = 1 : N % Calculate the Euclidean distance in any dimension (found with 2-norm) dists(idx) = norm(features' - dctData(idx,:), 2); end % Sort the distances in ascending order [sDists, origIdx] = sort(dists); % Find the corresponding best subjects for the shortest distances bestSubjects = zeros(k,1); for idx = 1 : k bestSubjects(idx) = dctDataClass(origIdx(idx)); end bestSubject = mode(bestSubjects);