Documentation Center

  • Trial Software
  • Product Updates

rangesearch

Class: ExhaustiveSearcher

Find all neighbors within specified distance using ExhaustiveSearcher object

Syntax

idx = rangesearch(NS,Y,r)
[idx,D]= rangesearch(NS,Y,r)
[idx,D]= rangesearch(NS,Y,r,Name,Value)

Description

idx = rangesearch(NS,Y,r) finds all points in NS.X that are within distance r of the Y points. Rows of NS.X and Y correspond to observations, and columns correspond to variables.

[idx,D]= rangesearch(NS,Y,r) returns the distances between each row of Y and the rows of NS.X that are r or less distant.

[idx,D]= rangesearch(NS,Y,r,Name,Value) finds nearby points with additional options specified by one or more Name,Value pair arguments.

Tips

  • For a fixed positive integer K, knnsearch finds the K points in NS.X that are nearest each Y point. In contrast, for a fixed positive real value r, rangesearch finds all the points in NS.X that are within a distance r of each Y point.

Input Arguments

NS

ExhaustiveSearcher object, constructed using ExhaustiveSearcher or createns.

Y

my-by-n numeric matrix, where each row represents one n-dimensional point. The number of columns n must equal the number of columns in NS.X.

r

Search radius, a scalar. rangesearch finds all NS.X points (rows) that are within distance r of each Y point. The meaning of distance depends on the Distance name-value pair.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'Cov'

Positive definite matrix indicating the covariance matrix when computing the Mahalanobis distance. This argument is only valid when the Distance name-value pair is 'mahalanobis'.

Default: nancov(X)

'Distance'

String or function handle specifying the distance metric.

ValueDescription
'euclidean'Euclidean distance.
'seuclidean'Standardized Euclidean distance. Each coordinate difference between X and a query point is scaled, meaning divided by a scale value S. The default value of S is the standard deviation computed from X, S=nanstd(X). To specify another value for S, use the Scale name-value pair.
'mahalanobis'Mahalanobis distance, computed using a positive definite covariance matrix C. The default value of C is the sample covariance matrix of X, as computed by nancov(X). To specify a different value for C, use the 'Cov' name-value pair.
'cityblock'City block distance.
'minkowski'Minkowski distance. The default exponent is 2. To specify a different exponent, use the 'P' name-value pair.
'chebychev'Chebychev distance (maximum coordinate difference).
'cosine'One minus the cosine of the included angle between observations (treated as vectors).
'correlation'One minus the sample linear correlation between observations (treated as sequences of values).
'hamming'Hamming distance, percentage of coordinates that differ.
'jaccard'One minus the Jaccard coefficient, the percentage of nonzero coordinates that differ.
'spearman'One minus the sample Spearman's rank correlation between observations (treated as sequences of values).
@distfunDistance function handle. distfun has the form
function D2 = DISTFUN(ZI,ZJ)
...
where
  • ZI is a 1-by-N vector containing one row of X or Y.

  • ZJ is an M2-by-N matrix containing multiple rows of X or Y.

  • D2 is an M2-by-1 vector of distances, D2(k) is the distance between the observations ZI and ZJ(J,:).

For definitions, see Distance Metrics.

Default: 'euclidean'

'P'

Positive scalar indicating the exponent of Minkowski distance. This argument is only valid when the Distance name-value pair is 'minkowski'.

Default: 2

'Scale'

Vector S containing nonnegative values, with length equal to the number of columns in X. Each coordinate difference between X and a query point is scaled by the corresponding element of S. This argument is only valid when the Distance name-value pair is 'seuclidean'.

Default: nanstd(X)

Output Arguments

idx

my-by-1 cell array, where my is the number of rows in Y. idx{I} contains the indices of points (rows) in NS.X whose distances to Y(I,:) are not greater than r. The entries in idx{I} are in ascending order of distance.

D

my-by-1 cell array, where my is the number of rows in Y. D{I} contains the distance values between Y(I,:) and the corresponding points in idx{I}.

Definitions

Distance Metrics

For definitions, see Distance Metrics.

Examples

Create X and Y as samples of 5-D normally distributed variables. Create an ExhaustiveSearcher object NS from X. Find the points in NS.X that are within a Euclidean distance 1.5 of each point in Y.

rng('default') % for reproducibility
X = randn(100,5);
Y = randn(10,5);
NS = ExhaustiveSearcher(X);
[idx, dist] = rangesearch(NS,Y,1.5)

idx = 
    [1x7  double]
    [1x2  double]
    [1x11 double]
    [1x2  double]
    [1x12 double]
    [1x9  double]
    [         89]
    [1x0  double]
    [1x0  double]
    [1x0  double]

dist = 
    [1x7  double]
    [1x2  double]
    [1x11 double]
    [1x2  double]
    [1x12 double]
    [1x9  double]
    [     1.1739]
    [1x0  double]
    [1x0  double]
    [1x0  double]

In this case, the last three points in Y are more than 1.5 distant from any point in NS.X. NS.X(89,:) is 1.1739 distant from Y(7,:), and there is no other point in NS.X that is within distance 1.5 of Y(7,:). There are 12 points in NS.X within distance 1.5 of Y(5,:).

Algorithms

The exhaustive search algorithm finds the distance of each point in X to each point in Y.

Alternatives

rangesearch is the ExhaustiveSearcher method for distance search. It is equivalent to the rangesearch function with the NSMethod name-value pair set to 'exhaustive'.

rangesearch is the KDTreeSearcher method for distance search. It is equivalent to the rangesearch function with the NSMethod name-value pair set to 'kdtree'.

See Also

| | | |

Was this topic helpful?