knn
KNN (k-nearest-neighbor) search
Contents
Syntax
- [nnIndex, nnDistance] = knn(mat1, mat2, opt)
Description
nnIndex = knn(mat1, mat2) returns the results of KNN search, where
- mat1: the test vectors (represented by columns)
- mat2: the vectors to be searched (represented by columns)
- nnIndex: the KNN indices into mat2 (each column is the result of each column in mat1)
[nnIndex, nnDistance] = knn(mat1, mat2) returns the KNN indices together with their distances (sorted)
[...] = knn(mat1, mat2, opt) takes extra options for KNN search, with
- opt.method: Method for distance computation
- opt.nnCount: K in KNN
- opt.skipIndex: Skip index into mat2 (Used for cross-validation)
Example
mat1=[2 2; 4 6]; % Columns 3 and 5 of mat2 mat2=[3 4 2 3 2; 2 2 4 5 6]; opt=knn('defaultOpt'); opt.nnCount=3; opt.skipIndex=[5]; pairwiseDist=distPairwise(mat1, mat2); [nnIndex, nnDistance]=knn(mat1, mat2, opt); disp(nnIndex), disp(nnDistance), disp(pairwiseDist)
3 4 4 3 1 1 0 1.4142 1.4142 2.0000 2.2361 4.1231 2.2361 2.8284 0 1.4142 2.0000 4.1231 4.4721 2.0000 1.4142 0