Categories &

Functions List

Function Reference: createns

Function File: obj = createns (X)
Function File: obj = createns (X, name, value, …)

Create a nearest neighbor searcher object.

obj = createns (X) creates a nearest neighbor searcher object using the training data X. By default, it constructs an ExhaustiveSearcher object with the Euclidean distance metric.

obj = createns (X, name, value, …) allows customization of the searcher type and its properties through name-value pairs. The following name-value pair is supported to specify the searcher type:

NameValue
"NSMethod"Specifies the nearest neighbor search method. Possible values are:
  • "exhaustive": Creates an ExhaustiveSearcher object.
  • "kdtree": Creates a KDTreeSearcher object.
  • "hnsw": Creates an hnswSearcher object.

Default is "exhaustive".

Additional name-value pairs depend on the selected "NSMethod" and are passed directly to the constructor of the corresponding class:

  • For "exhaustive", see ExhaustiveSearcher documentation for parameters like "Distance", "P", "Scale", and "Cov".
  • For "kdtree", see KDTreeSearcher documentation for parameters like "Distance", "P", and "BucketSize".
  • For "hnsw", see hnswSearcher documentation for parameters like "Distance", "P", "Scale", "Cov", "MaxNumLinksPerNode", and "TrainSetSize".

Input Arguments:

  • X - Training data, specified as an N×P numeric matrix where rows represent observations and columns represent features. Must be finite and numeric.

Output:

  • obj - A nearest neighbor searcher object of type ExhaustiveSearcher, KDTreeSearcher, or hnswSearcher, depending on the specified "NSMethod".

Examples:

 
 ## Create an ExhaustiveSearcher with default parameters
 X = [1, 2; 3, 4; 5, 6];
 obj = createns (X);

 ## Create a KDTreeSearcher with Euclidean distance
 obj = createns (X, "NSMethod", "kdtree", "Distance", "euclidean");

 ## Create an hnswSearcher with Minkowski distance and custom parameters
 obj = createns (X, "NSMethod", "hnsw", "Distance", "minkowski", "P", 3, "MaxNumLinksPerNode", 2);
 

See also: ExhaustiveSearcher, KDTreeSearcher, hnswSearcher, knnsearch, rangesearch

Source Code: createns