Main Content

nearestNeighbor

指定の点に最も近い頂点

説明

ID = nearestNeighbor(TR,P) は、P のクエリ点に最も近い頂点の ID を返します。行列 P の各行は、クエリ点の座標を含んでいます。

ID = nearestNeighbor(TR,x,y) は、2 次元クエリ点の x 座標と y 座標を個別の列ベクトルとして指定します。

ID = nearestNeighbor(TR,x,y,z) は、3 次元クエリ点の x 座標、y 座標、z 座標を個別の列ベクトルとして指定します。

[ID,d] = nearestNeighbor(___) は、前述の任意の構文で、各クエリ点とその最近傍点の間のユークリッド距離も返します。

すべて折りたたむ

3 次元三角形分割の最近傍点を計算します。

3 次元 Delaunay 三角形分割を作成します。

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*')

Figure contains an axes object. The axes object contains 2 objects of type patch, line. One or more of the lines displays its values using only markers

クエリ点の最近傍点の座標を検出します。

ID = nearestNeighbor(TR,x,y,z);
C = TR.Points(ID,:)
C = 1×3

     0     0     2

2 次元三角形分割の最近傍点を計算します。

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);

2 つのクエリ点を定義します。

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])

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

クエリ点の最近傍点とそれらの間の距離を求めます。

[ID,d] = nearestNeighbor(TR,P);

クエリ点の最近傍点である三角形分割の点を赤色で強調表示します。

N = TP(ID,:);
plot(N(:,1),N(:,2),'*r')

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers

各クエリ点とその最近傍点の間の距離を表示します。

d
d = 2×1

    1.1180
    1.5811

入力引数

すべて折りたたむ

三角形分割の表現。スカラー triangulation または delaunayTriangulation オブジェクトとして指定します。nearestNeighbor は制約付きエッジがある delaunayTriangulation オブジェクトをサポートしません。

データ型: triangulation | delaunayTriangulation

クエリ点。2 列または 3 列の行列として指定します。P にはクエリ点の x 座標、y 座標、および (該当する場合は) z 座標が格納されます。

データ型: double

クエリ点の x 座標。列ベクトルとして指定します。

データ型: double

クエリ点の y 座標。列ベクトルとして指定します。

データ型: double

クエリ点の z 座標。列ベクトルとして指定します。

データ型: double

出力引数

すべて折りたたむ

クエリ点の最近傍点の頂点 ID。列ベクトルとして返されます。頂点 ID は Points プロパティの対応する頂点の行番号です。

データ型: double

クエリ点からその最近傍点までのユークリッド距離。ID と同じ長さの列ベクトルとして返されます。

データ型: double

拡張機能

スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。

バージョン履歴

R2013a で導入