nearestNeighbor
最靠近指定点的顶点
语法
说明
示例
三维查询
计算三维三角剖分中的最近邻点。
创建三维德劳内三角剖分。
P = [1 1 0; -1 1 0; -1 -1 0; 1 -1 0; 0 0 2; 0 0 0]; TR = delaunayTriangulation(P);
绘图三角剖分和查询点。
tri = TR(:,:); trisurf(tri,P(:,1),P(:,2),P(:,3),'FaceAlpha',0.5) hold on x = 0; y = -0.5; z = 2; plot3(x,y,z,'k*')
查找查询点的最近邻点的坐标。
ID = nearestNeighbor(TR,x,y,z); C = TR.Points(ID,:)
C = 1×3
0 0 2
二维查询
计算二维三角剖分中的最近邻点。
创建二维三角剖分。
C = [5 3 1; 3 2 1; 3 4 2; 4 6 2]; TP = [2.5 8.0; 6.5 8.0; 2.5 5.0; 6.5 5.0; 1.0 6.5; 8.0 6.5]; TR = triangulation(C,TP);
定义两个查询点。
P = [2 4; 6 6.5];
绘图三角剖分和查询点。
triplot(TR) hold on plot(P(:,1),P(:,2),'k*') ylim([1.5 8.5]) xlim([0.5 8.5])
查找查询点的最近邻点以及它们之间的距离。
[ID,d] = nearestNeighbor(TR,P);
用红色突出显示三角剖分中查询点的最近邻点。
N = TP(ID,:);
plot(N(:,1),N(:,2),'*r')
显示每个查询点与其最近邻点之间的距离。
d
d = 2×1
1.1180
1.5811
输入参数
TR
— 三角剖分表示法
标量三角剖分对象
三角剖分表示,指定为标量 triangulation
或 delaunayTriangulation
对象。nearestNeighbor
不支持具有约束边的 delaunayTriangulation
对象。
数据类型: triangulation
| delaunayTriangulation
P
— 查询点
2 列矩阵(二维) | 3 列矩阵(三维)
查询点,指定为包含两列或三列的矩阵。P
包含查询点的 x 坐标和 y 坐标,还可能包含 z 坐标。
数据类型: double
x
— x 坐标
列向量
查询点的 x 坐标,指定为列向量。
数据类型: double
y
— y 坐标
列向量
查询点的 y 坐标,指定为列向量。
数据类型: double
z
— z 坐标
列向量
查询点的 z 坐标,指定为列向量。
数据类型: double
输出参量
ID
— 顶点 ID
列向量
查询点的最近邻点的顶点 ID,以列向量形式返回。顶点 ID 是 Points
属性中对应顶点的行号。
数据类型: double
d
— 欧几里德距离
列向量
从查询点到其最近邻点的欧几里德距离,以长度与 ID
相同的列向量形式返回。
数据类型: double
扩展功能
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
版本历史记录
在 R2013a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)