Syntax
nn=VLnearestNeighbor(VL1,VL2)
Input Parameter
VL1 : | | Vertex list or Point List [m x 3] |
VL2 : | | Vertex list or Point List [n x 3] |
Output Parameter
nn : | | Index in VL1 which is nearest to VL2 [n x 1] |
Examples
VLnearestNeighbor(rand(10,2),rand(10,2))
VLnearestNeighbor(rand(10,3),rand(10,3))
Copyright 2021-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This algorithm is designed to find the nearest points in one vertex list (VL1) to the points in another vertex list (VL2). It is part of the SolidGeometry library and was introduced in version 5.1.
Input Parameters
- VL1: A vertex list or point list with dimensions [m x 3].
- VL2: A vertex list or point list with dimensions [n x 3].
Output Results
- nn: An index array of size [n x 1] indicating the nearest points in VL1 to each point in VL2.
- nd: The Euclidean distance from each point in VL2 to its nearest point in VL1.
- pp: The actual nearest points in VL1 corresponding to each point in VL2.
Algorithm Steps
- Check if VL1 and VL2 have two columns. If so, add a third column of zeros using the function
VLaddz
to convert them to 3D points.
- Suppress specific warnings related to triangulation in MATLAB.
- Create a triangulation object
TR
using the points in VL1.
- Use the
nearestNeighbor
function to find the nearest points in VL1 for each point in VL2. Store the indices in nn
.
- Calculate the Euclidean distance
nd
between each point in VL2 and its nearest point in VL1 using the vecnormr
function.
- Store the nearest points in VL1 corresponding to each point in VL2 in
pp
.
- If no output arguments are specified, plot the points and their connections using the
SGfigure
, VLplot
, and lplot
functions.
Example Usage
To find the nearest neighbors between two random sets of 2D or 3D points:
VLnearestNeighbor(rand(10,2),rand(10,2))
VLnearestNeighbor(rand(10,3),rand(10,3))
Algorithm explaination created using ChatGPT on 2025-08-19 07:09. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21