This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.


Class: pointCloud

Find nearest neighbors of a point


[indices,dists] = findNearestNeighbors(ptCloud,point,K)
[indices,dists] = findNearestNeighbors(ptCloud,point,K,Name, Value)


[indices,dists] = findNearestNeighbors(ptCloud,point,K) returns the K nearest neighbors of a query point within the input point cloud. The point input is an [X,Y,Z] vector.

[indices,dists] = findNearestNeighbors(ptCloud,point,K,Name, Value) uses additional options specified by one or more Name,Value arguments.

Input Arguments

expand all

Point cloud, specified as a pointCloud object.

Query point, specified as an [X,Y,Z] vector.

Number of nearest neighbors, specified as a positive integer.

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.

expand all

Sort indices, specified as 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 sorting off, set Sort to false.

Number of leaf nodes, specified as an integer. Set MaxLeafChecks to the number of leaf nodes to search in the Kdtree. 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.

Output Arguments

expand all

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

Distances to query point, returned as a column vector. The dists contains K Euclidean distances to the query point.


expand all

Create a point cloud object with randomly generated points.

   ptCloud = pointCloud(1000*rand(100,3,'single'));

Define a query point and the number of neighbors.

   point = [50,50,50];
   K = 10;

Get the indices and the distances of 10 nearest points.

   [indices,dists] = findNearestNeighbors(ptCloud,point,K);


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.

See Also

Introduced in R2015a