Main Content


Find nearest neighbors of a point in point cloud


[indices,dists] = findNearestNeighbors(ptCloud,point,K) returns the indices for the K-nearest neighbors of a query point in the input point cloud. ptCloud can be an unorganized or organized point cloud. The K-nearest neighbors of the query point are computed by using the Kd-tree based search algorithm.

[indices,dists] = findNearestNeighbors(___,Name,Value) specifies options using one or more name-value arguments in addition to the input arguments in the preceding syntaxes.

Input Arguments

collapse all

Point cloud, specified as a pointCloud object.

Query point, specified as a three-element vector of form [x,y,z].

Number of nearest neighbors, specified as a positive integer.

Name-Value 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 quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: findNearestNeighbors(ptCloud,point,k,'Sort',true)

Sort indices, specified as a comma-separated pair of 'Sort' and a logical scalar. When you set Sort to true, the returned indices are sorted in the ascending order based on the distance from a query point. To turn off sorting, set Sort to false.

Number of leaf nodes to check, specified as a comma-separated pair consisting of 'MaxLeafChecks' and an integer. When you set this value to Inf, the entire tree is searched. When the entire tree is searched, it produces exact search results. Increasing the number of leaf nodes to check increases accuracy, but reduces efficiency.


The name-value argument 'MaxLeafChecks' is valid only with Kd-tree based search method.

Output Arguments

collapse all

Indices of stored points, returned as a column vector. The vector contains K linear indices of the nearest neighbors stored in the point cloud.

Distances to query point, returned as a column vector. The vector contains the Euclidean distances between the query point and its nearest neighbors.


[1] Muja, M. and David G. Lowe. "Fast Approximate Nearest Neighbors with Automatic Algorithm Configuration". In VISAPP International Conference on Computer Vision Theory and Applications. 2009. pp. 331–340.

Extended Capabilities

See Also

Introduced in R2020b