Nearest neighbor classifier🔗

Implements the (k) nearest neighbors classification algorithm. The algorithm compares a query vector to a fixed set of model vectors and returns the class label of the model vector that is closest to the query. If k is set to a value greater than one, the class labels of the k nearest model vectors will be found. The label with the largest number of occurrences will be selected as the winner.


  • queryVector: A 1-by-N matrix representing a vector to classify.

  • modelVectors: A M-by-N matrix that contains M N-dimensional model vectors.

  • classLabels: A M-by-1 matrix that contains a class label for each model vector. If classLabels is an empty matrix, the index of the model vector will be used as the class label. If k is larger than 1, classLabels must be set.

  • k: The number of closest model vectors to consider.

  • distanceMeasure: The measure according to which the closest model vector is found.


  • classLabel: The label of the model vector closest to input (k = 1) or the label with the most occurrences within k closest vectors (k > 1).

  • modelIndex: The index of the closest model vector. If k is greater than one, this will be the index of the closest model vector with a winning class label.

  • distance: The distance to model vector at modelIndex.

enum DistanceMeasure🔗

Supported distance measures.


enumerator Euclidean🔗

The inputs are considered points in a Cartesian coordinate system.

The distance between them is calculated using the Pythagorean theorem.

enumerator HistogramIntersection🔗

The inputs are considered statistical distributions.

The distance between them is calculated as the (negated) intersection between two distributions.