NearestTo
更多信息和选项
- NearestTo 适用于各种数据,包括数字、地理空间数据、文本和视觉数据,以及日期和时间.
- NearestTo[x][elems] 等价于 NearestTo[elems,x].
- NearestTo[x,nr][elems] 等价于 NearestTo[elems,x,nr],其中 nr 是一个整数 n 或数据对 {n,r}.
- NearestFunction 对象 nf 的 NearestTo[x][nf] 等价于 nf[x].
- NearestFunction 对象 nf 的 NearestTo[x,nr][nf] 等价于 nf[x,nr],其中 nr 是一个整数 n 或数据对 {n,r}.
- NearestTo 接受以下选项:
-
DistanceFunction Automatic 要使用的距离度量标准 Method Automatic 使用方法 WorkingPrecision Automatic 用于数值数据的精度
范例
打开所有单元 关闭所有单元基本范例 (4)
NearestTo[20][{1, 2, 4, 8, 16, 32}]NearestTo[20, 3][{1, 2, 4, 8, 16, 32}]NearestTo[{2, 3}][{{-1, -1}, {1, -1}, {1, 1}, {-1, 1}}]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]};NearestTo[Red, 3][l]NearestTo["aaba", 3][{"aaaa", "abaa", "bbbb", "aaaba"}]EditDistance[#, "aaba"]& /@ {"aaaa", "abaa", "bbbb", "aaaba"}范围 (6)
NearestTo[5, 3][{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]NearestTo[5, {All, 2}][{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]NearestTo[5, {3, 2}][{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]对 NearestFunction 表达式应用 NearestTo:
nf = Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]NearestTo[5, {3, 2}][nf]nf[5, {3, 2}]NearestTo["David"][{"I'm", "sorry", "Dave"} -> "Index"]返回一个 Association,给出字符串元素、索引和距离:
NearestTo["David"][{"I'm", "sorry", "Dave"} -> All]找出最接近 20 的 3 个元素,同时给出元素和它们到 20 的距离:
NearestTo[20, 3][{1, 2, 4, 8, 16, 32} -> {"Element", "Distance"}]找出二维空间中离 {2,3} 最近的元素,并返回相应的标签:
NearestTo[{2, 3}][{{-1, -1} -> a, {1, -1} -> b, {1, 1} -> c, {-1, 1} -> d}]用 Association 算出同样的结果:
NearestTo[{2, 3}][<|a -> {-1, -1}, b -> {1, -1}, c -> {1, 1}, d -> {-1, 1}|>]EntityClass["Element", "Density" -> NearestTo[Quantity[4.5, "Grams" / "Centimeters" ^ 3]]]EntityList[%]选项 (1)
DistanceFunction (1)
NearestTo[ {0, 0}][{{1.5, .6}, {2, 0}, {1.25, 1.25}}]使用 ManhattanDistance,它会将每个边的长度加在一起:
NearestTo[{0, 0}, DistanceFunction -> ManhattanDistance][{{1.5, .6}, {2, 0}, {1.25, 1.25}}]ChessboardDistance 只考虑间隔最大的维度:
NearestTo[{0, 0}, DistanceFunction -> ChessboardDistance][{{1.5, .6}, {2, 0}, {1.25, 1.25}}]应用 (3)
nf = Nearest[DictionaryLookup[]]NearestTo["fish", 5][nf]i = [image];subimage = [image];NearestTo[subimage][Flatten@ImagePartition[i, 45]]polygon = EntityValue[Entity["Country", "Mexico"], "Polygon"]nearest = NearestTo[Here][polygon[[1]]]GeoGraphics[{Arrow[GeoPath[{Here, nearest[[1]]}]], EdgeForm[Black], polygon}]文本
Wolfram Research (2018),NearestTo,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NearestTo.html.
CMS
Wolfram 语言. 2018. "NearestTo." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/NearestTo.html.
APA
Wolfram 语言. (2018). NearestTo. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NearestTo.html 年
BibTeX
@misc{reference.wolfram_2026_nearestto, author="Wolfram Research", title="{NearestTo}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/NearestTo.html}", note=[Accessed: 02-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_nearestto, organization={Wolfram Research}, title={NearestTo}, year={2018}, url={https://reference.wolfram.com/language/ref/NearestTo.html}, note=[Accessed: 02-November-2026]}