Nearest neighbors search using exhaustive search
An ExhaustiveSearcher object performs kNN (k-nearest neighbor) search using exhaustive search. Search objects store information about the data used, and the distance metric and parameters. The search performance for this object, compared with the KDTreeSearcher object, tends to be better for larger dimensions (10 or more) and worse for smaller dimensions. For details on search objects, see What Are Search Objects?.
NS = ExhaustiveSearcher(X,'Name',Value) constructs an ExhaustiveSearcher object based on X, where rows of X correspond to observations and columns correspond to variables, using one or more optional name-value pair arguments. You can then use this object to find neighbors in X nearest to the query points.
NS = createns(X,'NSMethod','exhaustive','Name',Value) creates an ExhaustiveSearcher object based on X using createns, where rows of X correspond to observations and columns correspond to variables, using one or more optional name-value pair arguments. You can use this object to find neighbors in X nearest to the query points.
The ExhaustiveSearcher and the createns functions accept one or more of the following optional name-value pair arguments as input.
A string or function handle specifying the default distance metric used when you call the knnsearch method.
For details on these distance metrics, see Distance Metrics.
A positive scalar indicating the exponent of the Minkowski distance. This parameter is only valid when Distance is 'minkowski'. Default is 2.
A positive definite matrix indicating the covariance matrix when computing the Mahalanobis distance. This parameter is only valid when Distance is 'mahalanobis'. Default is nancov(X).
A vector S with the length equal to the number of columns in X. Each coordinate of X and each query point is scaled by the corresponding element of S when computing the standardized Euclidean distance. This parameter is only valid when Distance is 'seuclidean'. Default is nanstd(X).
A matrix used to create the object.
A string specifying a built-in distance metric or a function handle that you provide when you create the object. This property is the default distance metric used when you call the knnsearch method to find nearest neighbors for future query points.
Specifies the additional parameter for the chosen distance metric. The value is:
|knnsearch||Find k-nearest neighbors using ExhaustiveSearcher object|
|rangesearch||Find all neighbors within specified distance using ExhaustiveSearcher object|
You can create an ExhaustiveSearcher model in two ways.
Load Fisher's iris data. Focus on the petal dimensions.
load fisheriris X = meas(:,[3 4]); % Predictors
Prepare an exhaustive, nearest neighbors searcher using ExhaustiveSearcher and the predictors. Use the Minkowski distance metric.
ESMdl1 = ExhaustiveSearcher(X,'Distance','Minkowski')
ESMdl1 = ExhaustiveSearcher with properties: Distance: 'minkowski' DistParameter: 2 X: [150x2 double]
ESMdl1 is an ExhaustiveSearcher model. Access properties of ESMdl1 using dot notation. For example, use ESMdl1.DistParameter to access the Minkowski distance exponent.
Prepare an exhaustive, nearest neighbors searcher using createns and by specifying that the nearest neighbors search method is exhaustive.
ESMdl2 = createns(X,'NsMethod','exhaustive',... 'Distance','minkowski')
ESMdl2 = ExhaustiveSearcher with properties: Distance: 'minkowski' DistParameter: 2 X: [150x2 double]
ESMdl2 is also an ExhaustiveSearcher model, and it is equivalent to ESMdl1.
You can pass new data and either of the models to:
ExhaustiveSearcher.knnsearch to find indices and distances of nearest neighbors.
ExhaustiveSearcher.rangesearch to find indices of all nearest neighbors within a distance that you specify.
 Friedman, J. H., Bentely, J. and Finkel, R. A. (1977). An Algorithm for Finding Best Matches in Logarithmic Expected Time, ACM Transactions on Mathematical Software 3, 209.