Nearest[{elem1,elem2,…},x]
x に最も近い elemiのリストを与える.
Nearest[{elem1v1,elem2v2,…},x]
x に最も近い elemiに対応する viを与える.
Nearest[{elem1,elem2,…}{v1,v2,…},x]
同じ結果を与える.
Nearest[{elem1,elem2,…}prop,x]
x が最も近い elemiの特性 prop を与える.
Nearest[data,{x1,x2,…}]
事実上,{Nearest[data,x1],Nearest[data,x2],…}を与える.
Nearest[data,x,n]
x に最も近い n 個の elemiを与える.
Nearest[data,x,{n,r}]
x の半径 r 内にある,x に最も近い n 個以下の elemiを与える.
Nearest[data]
異なる x に繰り返し適用可能なNearestFunction[…]を生成する.
Nearest
Nearest[{elem1,elem2,…},x]
x に最も近い elemiのリストを与える.
Nearest[{elem1v1,elem2v2,…},x]
x に最も近い elemiに対応する viを与える.
Nearest[{elem1,elem2,…}{v1,v2,…},x]
同じ結果を与える.
Nearest[{elem1,elem2,…}prop,x]
x が最も近い elemiの特性 prop を与える.
Nearest[data,{x1,x2,…}]
事実上,{Nearest[data,x1],Nearest[data,x2],…}を与える.
Nearest[data,x,n]
x に最も近い n 個の elemiを与える.
Nearest[data,x,{n,r}]
x の半径 r 内にある,x に最も近い n 個以下の elemiを与える.
Nearest[data]
異なる x に繰り返し適用可能なNearestFunction[…]を生成する.
詳細とオプション
- Nearestは,数値データ,地理空間データ,テキストデータ,視覚データを含むさまざまなデータおよび日付や時刻に使うことができる.
- データは連想として与えることもできる.この場合,Nearest[<|key1val1,key2val2,…|>]はNearest[{val1key1,val2key2,…}]に等しい.
- 以下は,Nearest[{elem1,elem2,…}prop,…]で prop の取り得る形である.
-
"Element" 最も近いことが分かった elemi "Index" 最も近いことが分かった elemiの指標 i "Distance" 最も近い elemiまでの距離 {prop1,prop2,…} 複数の形式のリスト All 要素,指標,距離を与える連想 - Nearestが複数の要素 elemiを返す場合が,最も近いものが先頭に置かれる.
- 複数の要素が等距離にある場合は,data 中の出現順で返される.
- Nearest[data,x,{All,r}]を使って半径 r 内のすべての elemiを得ることができる.
- 次のオプションが与えられる.
-
DistanceFunction Automatic 使用する距離尺度 Method Automatic 使用するメソッド WorkingPrecision Automatic 数値データに使用する精度 - デフォルトで,異なるタイプの elemiに次の距離関数を使うことができる.
-
Norm[#1-#2]& 数値データ JaccardDissimilarity ブールデータ EditDistance 文字列 ColorDistance 色 ImageDistance 画像 DateDifference 日付と時刻 GeoDistance 地理空間データ - 地理空間データを伴うNearestはGeoDistance使って距離を計算する.データはGeoPositionオブジェクトのリストとして,あるいは点の配列を含むGeoPositionとして与えることができる.
- 画像あるいは色および距離関数 f について,DistanceFunction->f はそれぞれImageDistanceおよびColorDistanceに渡される. »
- 画像はすべてはConformImagesを使って統一される.DistanceFunction->Automaticのとき,離散余弦変換に基づく次元縮退が画像の集合に適用される.
についてNorm[#1-#2,p]&またはManhattanDistance,ChessboardDistance,EuclideanDistance等の名前付きの距離関数を使うと,数値ベクトルデータに対しての特別の最適化が呼び出される.- Methodに使用可能な設定値には"Octree","KDtree","Scan"がある.
- WorkingPrecisionオプションの使用可能な設定値
-
MachinePrecision 機械精度数を使う p 精度 p を使う Automatic 最近点を求めるために適応的精度を使う
例題
すべて開く すべて閉じる例 (5)
Nearest[{1, 2, 4, 8, 16, 32}, 20]Nearest[{1, 2, 4, 8, 16, 32}, 20, 3]Nearest[{{-1, -1}, {1, -1}, {1, 1}, {-1, 1}}, {2, 3}]Nearest[{"aaaa", "abaa", "bbbb", "aaaba"}, "aaba", 3]EditDistance[#, "aaba"]& /@ {"aaaa", "abaa", "bbbb", "aaaba"}l = {RGBColor[1., 0.7573220867762727, 0.8225376261211264], RGBColor[1., 0.9053403248427643, 0.21717287340883917], RGBColor[1., 0.5544801460824762, 0.12056345655596812], RGBColor[1., 0.2818404077149421, 0.1073945311994069], RGBColor[1., 0.12423838985259317, 0.19023691956664956], RGBColor[0.8, 0.4542154246540884, 0.31688034954543], RGBColor[0.8, 0.5483770742736782, 0.16977938137471082], RGBColor[0.8, 0.03163746197875539, 0.5781619271042624], RGBColor[0.8, 0.1612089376881538, 0.15737556414394493], RGBColor[0.8, 0.947661405438111, 0.8404962405438101], RGBColor[0.5, 0.8592283961197744, 0.04768022523989446], RGBColor[0.5, 0.04821552495323611, 0.9686564512239644], RGBColor[0.5, 0.5966778129134023, 0.8001648440588174], RGBColor[0.5, 0.5307402622478448, 0.7029200788120107], RGBColor[0.5, 0.6654316628707297, 0.9850955091132039]};Nearest[l, Red, 3]i = [image];subimage = [image];Nearest[Flatten@ImagePartition[i, 45], subimage]スコープ (9)
Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 3]Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, {All, 2}]Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, {3, 2}]Nearest[Table[PauliMatrix[i], {i, 3}], {{1, -2}, {3, -4}}]Nearest[{{-1, -1} -> a, {1, -1} -> b, {1, 1} -> c, {-1, 1} -> d}, {2, 3}]Associationを使って同じ計算をする:
Nearest[<|a -> {-1, -1}, b -> {1, -1}, c -> {1, 1}, d -> {-1, 1}|>, {2, 3}]Nearest[{"I'm", "sorry", "Dave"} -> "Index", "David"]文字列要素,指標,距離を与えるAssociationを返す:
Nearest[{"I'm", "sorry", "Dave"} -> All, "David"]20に最も近い要素を3つ求め,同時に要素と20までの距離をレポートする:
Nearest[{1, 2, 4, 8, 16, 32} -> {"Element", "Distance"}, 20, 3]一様ランダム分布に従う3Dの点について,原点に最も近い10の点の距離を与える:
points = RandomReal[{-1, 1}, {10000, 3}];
Nearest[points -> "Distance", {0, 0, 0}, 10]最も近い10の点のそれぞれについて,点要素,指標,距離を含むAssociationを与える:
Nearest[points -> All, {0, 0, 0}, 10] // TableFormnf = Nearest[{{-1, -1, 0} -> a, {1, -1, 1} -> b, {1, 1, 0} -> c, {-1, 1, 0} -> d}]nf[{2, 0, 0}]与えられたDateObjectに最も近いデータを求める:
dates = {DateObject[{2022, 3, 8, 2, 0, 0.}, "Instant", "Gregorian", -5.], DateObject[{2022, 5, 29, 9, 6, 7.64248}, "Instant", "Gregorian", -4.], DateObject[{2022, 8, 4, 18, 26, 14.}, "Instant", "Gregorian", 0.], DateObject[{2023, 1, 5, 20, 45, 19.2}, "Instant", "Gregorian", 2.]};Nearest[dates, Now]Nearest[{DateObject[{1500}, "Year", "Gregorian", -6.], DateObject[{1550}, "Year", "Gregorian", -6.], DateObject[{1600}, "Year", "Gregorian", -6.], DateObject[{1650}, "Year", "Gregorian", -6.]}, EntityValue[["galleleo"], "BirthDate"]]地球上の{lat,lon}点のいずれがユーザに最も近いかを求める:
points = GeoPosition[{{0, 0}, {0, 90}, {0, 180}, {0, -90}, {90, 0}, {-90, 0}}]HereNearest[points, Here]入力を別々のGeoPositionオブジェクトのリストとして表す:
Thread[points]Nearest[%, Here]Nearest[points -> {"Element", "Distance"}, Here]オプション (6)
DistanceFunction (3)
Nearest[{{1.5, .6}, {2, 0}, {1.25, 1.25}}, {0, 0}]各側の長さを合計するManhattanDistanceを用いる:
Nearest[{{1.5, .6}, {2, 0}, {1.25, 1.25}}, {0, 0}, DistanceFunction -> ManhattanDistance]ChessboardDistanceは最大距離間隔の次元のみを考慮する:
Nearest[{{1.5, .6}, {2, 0}, {1.25, 1.25}}, {0, 0}, DistanceFunction -> ChessboardDistance]DistanceFunctionは記号として与えることができる:
dist[{u_, v_}, {x_, y_}] := 3Abs[u - x] + 2Abs[v - y]Nearest[{{1.5, .6}, {2, 0}, {1.25, 1.25}}, {0, 0}, DistanceFunction -> dist]Nearest[{{1.5, .6}, {2, 0}, {1.25, 1.25}}, {0, 0}, DistanceFunction -> (3Abs[#1[[1]] - #2[[1]]] + 2Abs[#1[[2]] - #2[[2]]]&)]ColorDistanceにおけるデフォルト距離とは異なる色距離を使って最も近い色を求める:
l = {RGBColor[1., 0.7573220867762727, 0.8225376261211264], RGBColor[1., 0.9053403248427643, 0.21717287340883917], RGBColor[1., 0.5544801460824762, 0.12056345655596812], RGBColor[1., 0.2818404077149421, 0.1073945311994069], RGBColor[1., 0.12423838985259317, 0.19023691956664956], RGBColor[0.8, 0.4542154246540884, 0.31688034954543], RGBColor[0.8, 0.5483770742736782, 0.16977938137471082], RGBColor[0.8, 0.03163746197875539, 0.5781619271042624], RGBColor[0.8, 0.1612089376881538, 0.15737556414394493], RGBColor[0.8, 0.947661405438111, 0.8404962405438101], RGBColor[0.5, 0.8592283961197744, 0.04768022523989446], RGBColor[0.5, 0.04821552495323611, 0.9686564512239644], RGBColor[0.5, 0.5966778129134023, 0.8001648440588174], RGBColor[0.5, 0.5307402622478448, 0.7029200788120107], RGBColor[0.5, 0.6654316628707297, 0.9850955091132039]};Nearest[l, Red, 3, DistanceFunction -> "CIE2000"]Method (2)
methods = {Automatic, "Scan", "KDtree", "Octree"};data3 = RandomReal[1, {10 ^ 6, 3}];
test3 = RandomReal[1, {10 ^ 3, 3}];TableForm[Table[{
First[AbsoluteTiming[nf3 = Nearest[data3, Method -> m];]],
First[AbsoluteTiming[nf3[test3];]],
First[AbsoluteTiming[nf3[test3, 25];]]}, {m, methods}], TableHeadings -> {methods, {"Setup", "1 nearest", "25 nearest"}}]methods = {Automatic, "Scan", "KDtree"};data20 = RandomReal[1, {10 ^ 5, 20}];
test20 = RandomReal[1, {10 ^ 3, 20}];TableForm[Table[{
First[AbsoluteTiming[nf20 = Nearest[data20, Method -> m];]],
First[AbsoluteTiming[nf20[test20];]],
First[AbsoluteTiming[nf20[test20, 25];]]}, {m, methods}], TableHeadings -> {methods, {"Setup", "1 nearest", "25 nearest"}}]Method->{"KDtree","LeafSize"->s}の設定を使って,構築されたKDツリーの任意の葉の最大点数を制御することができる:
data = RandomReal[1, {10 ^ 6, 3}];
test = RandomReal[1, {10 ^ 4, 3}];times = Table[{s, First[AbsoluteTiming[nf = Nearest[data, Method -> {"KDtree", "LeafSize" -> s}];]], First[AbsoluteTiming[nf[test];]]}, {s, 50}];ListPlot[times[[All, {1, 2}]]]ListPlot[times[[All, {1, 3}]]]WorkingPrecision (1)
WorkingPrecision->MachinePrecisionを使うと,最も速い評価法が確実に使われる:
data = Pi + RandomInteger[{-9, 9}, {321, 4}];
test = E + RandomInteger[{-9, 9}, {23, 4}];First[AbsoluteTiming[Nearest[data, test]]]First[AbsoluteTiming[Nearest[data, test, WorkingPrecision -> MachinePrecision]]]数がすべて機械精度と区別できない場合は,結果は正しくないかもしれない:
data = 10 ^ 20 + RandomInteger[{-9, 9}, {321, 4}];
test = 10 ^ 20 + RandomInteger[{-9, 9}, {23, 4}];First[AbsoluteTiming[na = Nearest[data -> Automatic, test]]]First[AbsoluteTiming[nmp = Nearest[data -> Automatic, test, WorkingPrecision -> MachinePrecision]]]nmp === naDimensions[nmp]First[AbsoluteTiming[np = Nearest[data -> Automatic, test, WorkingPrecision -> 24]]]na === npアプリケーション (8)
リストのNearestをプロットする:
Plot[First[Nearest[{1, 3, 4, 7, 8}, x]], {x, 0, 10}]nf = Nearest[Table[{t Sin[t], t Cos[t]} -> t, {t, 5}]]DensityPlot[First[nf[{x, y}]], {x, -5, 5}, {y, -5, 5}]DensityPlot[First[nf[{x, y}]], {x, -5, 5}, {y, -5, 5}, PlotPoints -> 50]nf = Nearest[Table[{t Sin[t], t Cos[t]} -> t, {t, 5}], DistanceFunction -> (Norm[#1 - #2, 1]&)];DensityPlot[First[nf[{x, y}]], {x, -5, 5}, {y, -5, 5}, PlotPoints -> 50, Epilog -> {Red, Point[Table[{t Sin[t], t Cos[t]}, {t, 5}]]}]With[{data = RandomReal[{-1, 1}, {1000, 2}]}, ListPlot[{data, Nearest[data, {0, 0}, 200]}, PlotStyle -> {Blue, Red}]]With[{data = RandomReal[{-1, 1}, {1000, 2}]}, ListPlot[{data, Nearest[data, {0, 0}, 200, DistanceFunction -> (Norm[#1 - #2, 1]&)]}, PlotStyle -> {Blue, Red}]]nf = Nearest[DictionaryLookup[]]nf["pickel"]nf["pickel", 10]Table[First[CellularAutomaton[i, {{1}, 0}, {{3}, All}]] -> i, {i, 0, 4}]256の初歩的セルオートマトンすべての出力の完全リストを生成する:
ca = Table[First[CellularAutomaton[i, {{1}, 0}, {{5}, All}]] -> i, {i, 0, 255}];Nearest[ca, {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}]Nearest[ca, {1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1}]Nearest[{1, 2, 3, 4, 5}, 1, DistanceFunction -> (-Abs[#1 - #2]&)]Nearest[{1, 2, 3, 4, 5}, 5, DistanceFunction -> (-Abs[#1 - #2]&)]Plot[Nearest[{1, 2, 3, 4, 5}, x, DistanceFunction -> (-Abs[#1 - #2]&)], {x, 1, 5}]polygon = EntityValue[["Mexico"], "Polygon"]ユーザの現行の地理位置からその多角形に最も近い点を計算する:
nearest = Nearest[polygon[[1]], Here]GeoGraphics[{Arrow[GeoPath[{Here, nearest[[1]]}]], EdgeForm[Black], polygon}]特性と関係 (2)
Nearest[{5, 1, 13, 9, 16}, 3]Nearestの1引数形式はNearestFunctionオブジェクトを返す:
data = RandomReal[1, {10000}];
nf = Nearest[data]これは,繰り返しNearestを呼ぶよりも速い,最適化された検索関数である:
Do[nf[RandomReal[]], {500}] // AbsoluteTimingDo[Nearest[data, RandomReal[]], {500}] // AbsoluteTimingテクニカルノート
-
▪
- データのクラスタ分割 ▪
- Nearestを使う
関連するガイド
-
▪
- 論理演算とブール代数 ▪
- ブール計算 ▪
- 計算幾何学 ▪
- 教師あり機械学習 ▪
- テキスト解析 ▪
- 自然言語処理 ▪
- 配列アラインメントと比較 ▪
- 距離と類似度の測定基準 ▪
- 日付と時刻 ▪
- 機械学習 ▪
- 科学的モデル ▪
- 言語学データ ▪
- リストへの数学的およびカウント操作 ▪
- コンピュータビジョン ▪
- 音声解析 ▪
- 音声処理 ▪
- 音声計算
テキスト
Wolfram Research (2007), Nearest, Wolfram言語関数, https://reference.wolfram.com/language/ref/Nearest.html (2017年に更新).
CMS
Wolfram Language. 2007. "Nearest." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/Nearest.html.
APA
Wolfram Language. (2007). Nearest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Nearest.html
BibTeX
@misc{reference.wolfram_2026_nearest, author="Wolfram Research", title="{Nearest}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/Nearest.html}", note=[Accessed: 31-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_nearest, organization={Wolfram Research}, title={Nearest}, year={2017}, url={https://reference.wolfram.com/language/ref/Nearest.html}, note=[Accessed: 31-October-2026]}