DistanceMatrix[{u1,u2,…}]
给出每对元素 ui, uj 之间距离的矩阵.
DistanceMatrix[{u1,u2,…},{v1,v2,…}]
给出每对元素 ui, vj 之间距离的矩阵.
DistanceMatrix
DistanceMatrix[{u1,u2,…}]
给出每对元素 ui, uj 之间距离的矩阵.
DistanceMatrix[{u1,u2,…},{v1,v2,…}]
给出每对元素 ui, vj 之间距离的矩阵.
更多信息和选项
- DistanceMatrix 适用于各种数据,包括数值、地理空间、文字、视觉、日期和时间类型的数据,以及这些数据类型的组合.
- 每个 ui 可以是数据元素、数据元素列表或数据元素关联. 在 DistanceMatrix[data,…] 中,data 也可以是 Dataset 对象.
- 可以给出下列选项:
-
DistanceFunction Automatic 使用的距离度量 FeatureExtractor Identity 怎样预处理数据 FeatureNames Automatic 分配给数据的特征名称 FeatureTypes Automatic 假定的数据的特征类型 PerformanceGoal Automatic 试图优化的目标 RandomSeeding 1234 内部应怎样对伪随机数字生成器进行播种 WorkingPrecision Automatic 用于数值数据的精度 - DistanceFunction 的设置可以是任意距离或差异函数,或是定义两个值之间的距离的函数 f.
- 默认情况下,下列距离函数被用于不同类型的元素:
-
EuclideanDistance 数值数据 ImageDistance 图像 JaccardDissimilarity 布尔数据 EditDistance 字符串 Abs[DateDifference[#1,#2]]& 日期和时间 ColorDistance 颜色 GeoDistance 地理空间数据 Boole[SameQ[#1,#2]]& 标称数据 HammingDistance 标称向量数据 WarpingDistance 数值序列 - 对于图像、颜色或音频对象和距离函数 f,DistanceFunction->f 可分别传递给 ImageDistance、ColorDistance 或 AudioDistance. »
- 用 ConformImages 预先同化处理所有数据.
- 缺省情况下,当数据元素为混合类型的向量时,对每个类型单独计算距离,然后用 Norm 结合起来.
- PerformanceGoal 的可能设置包括:
-
"Speed" 最小化计算时间 "Quality" 最大化精度和准确度 Automatic 自动在速度和精度之间折衷处理 - RandomSeeding 的可能设置包括:
-
Automatic 每次调用函数时都自动重新播种 Inherited 使用外部播种的随机数字 seed 明确指定整数或字符串作为种子
范例
打开所有单元 关闭所有单元基本范例 (3)
dm = DistanceMatrix[{1, 2, 3, 4}]dm // MatrixFormdm = DistanceMatrix[{1, 2, 3, 4}, {5, 6, 7}]dm // MatrixFormdm = DistanceMatrix[{{1.5, 4.3}, {2.1, 5.6}, {6.2, 4.5}}, {{1.6, 4.2}, {2.3, 5.4}}]dm // MatrixForm范围 (10)
DistanceMatrix[{[image], [image], [image]}] // MatrixFormDistanceMatrix[{"abcd", "bcde", "xyz"}] // MatrixFormDistanceMatrix[{{True, False, True}, {True, True, True}, {True, False, False}}] // MatrixFormDistanceMatrix[{DateObject[{1985, 10, 3}], DateObject[{1989, 5, 30}], DateObject[]}] // MatrixFormDistanceMatrix[{GeoPosition[Entity["City", {"Paris", "IleDeFrance", "France"}]], GeoPosition[Entity["City", {"Sydney", "NewSouthWales", "Australia"}]], GeoPosition[Entity["City", {"Boston", "Massachusetts", "UnitedStates"}]], GeoPosition[Entity["City", {"SanFrancisco", "California", "UnitedStates"}]]}] // MatrixFormDistanceMatrix[{{"a", "b", "c", "d"}, {"b", "c", "d", "e"}, {"x", "y", "z"}}] // MatrixFormDistanceMatrix[{{1.1, 1.2, 1.3, 1.4, 1.5}, {1.2, 1.3}, {1.4, 1.5, 1.6}}] // MatrixFormDistanceMatrix[{{"A", "Sugar"}, {"A", "Salt"}, {"B", "Sugar"}, {"B", "Salt"}}]//MatrixFormDistanceMatrix[{{1.2, "A"}, {3.1, "A"}, {1.1, "B"}, {3.3, "B"}}] // MatrixFormdata = {<|"age" -> 32, "height" -> 160, "gender" -> "female"|>,
<|"height" -> 183, "age" -> 41, "gender" -> "female"|>,
<|"height" -> 123, "age" -> 30, "gender" -> "female"|>,
<|"height" -> 175, "age" -> 21, "gender" -> "male"|>,
<|"height" -> 150, "age" -> 11, "gender" -> "male"|>,
<|"age" -> 52, "height" -> 164, "gender" -> "female"|>};DistanceMatrix[data]//MatrixFormDistanceMatrix[<|"age" -> {32, 41, 30, 21, 11, 52}, "height" -> {160, 183, 123, 175, 150, 164}, "gender" -> {"female", "female", "female", "male", "male", "female"}|>]//MatrixForm还可以用 Dataset 对象给出数据:
dataset = Dataset[data]DistanceMatrix[dataset]//MatrixForm选项 (9)
DistanceFunction (3)
用 SquaredEuclideanDistance 作为距离函数计算整数向量间的距离矩阵:
DistanceMatrix[{{1, 5}, {2, 3}, {3, 1}, {4, 7}}, DistanceFunction -> SquaredEuclideanDistance] // MatrixForm用 ManhattanDistance 计算距离矩阵:
DistanceMatrix[{{1.3, 5.2}, {1.2, 7.3}, {3.6, 1.1}, {4.6, 7.4}}, DistanceFunction -> ManhattanDistance] // MatrixFormDistanceMatrix[{RGBColor[0.8153963685313876, 0.8780324414500478, 0.8878969785400366], RGBColor[0.7000498357974252, 0.12659377056191357, 0.2441846342253282], RGBColor[0.22075683601648843, 0.11406627268342273, 0.5155520370226843], RGBColor[0.7106648135050211, 0.42410292720247544, 0.14462936430464635], RGBColor[0.46490866303974765, 0.16254211516915973, 0.6466382153254229]}, DistanceFunction -> "CIE2000"]FeatureExtractor (1)
FeatureNames (1)
用 FeatureNames 命名特征,在以后的规范中用该名称来指代:
DistanceMatrix[{{2.3, "male"}, {4.8, Missing[]}, {Missing[], "female"}, {5.2, "female"}}, FeatureNames -> {"gender", "age"}, FeatureTypes -> <|"gender" -> "Numerical", "age" -> "Nominal"|>]FeatureTypes (1)
用 FeatureTypes 把第一个特征强制解释为标称特征:
DistanceMatrix[{{1, "A"}, {2, "A"}, {2, "B"}, {1, "B"}}, FeatureTypes -> <|1 -> "Nominal"|>]// MatrixFormPerformanceGoal (1)
vectors = RandomReal[{10, 11}, {2000, 1000}];AbsoluteTiming[result = DistanceMatrix[vectors];]将 PerformanceGoal 设为 "Speed",进行同样的计算:
AbsoluteTiming[resultspeed = DistanceMatrix[vectors, PerformanceGoal -> "Speed"];]AbsoluteTiming[reference = Outer[EuclideanDistance, vectors, vectors, 1];]StandardDeviation[Flatten[result - reference]]StandardDeviation[Flatten[resultspeed - reference]]当 PerformanceGoal"Speed" 时,将数据中心化会提高精度:
mean = Mean[vectors];
centered = # - mean & /@ vectors;AbsoluteTiming[resultspeedcentered = DistanceMatrix[centered, PerformanceGoal -> "Speed"];]StandardDeviation[Flatten[resultspeedcentered - reference]]RandomSeeding (1)
多次对 DistanceMatrix 进行计算给出同样的结果,即便是用到了随机数字.
data = RandomReal[1, {2, 20}];table = Table[DistanceMatrix[data, FeatureExtractor -> (RandomChoice[#, 10]&)], 5];Counts[table]对 RandomSeeding 选项给出不同的值,计算距离矩阵:
randomtable = Table[DistanceMatrix[data, FeatureExtractor -> (RandomChoice[#, 10]&), RandomSeeding -> RandomInteger[10]], 5];Counts[randomtable]WorkingPrecision (1)
计算500个长度为100、精度为30位的随机数值向量的距离矩阵:
r = RandomReal[1, {500, 100}, WorkingPrecision -> 30];AbsoluteTiming[dm = DistanceMatrix[r];]DistanceMatrix 使用任意精度的计算:
Precision[dm]设置 WorkingPrecisionMachinePrecision 可以加快计算速度:
AbsoluteTiming[dmmachine = DistanceMatrix[r, WorkingPrecision -> MachinePrecision];]Precision[dmmachine]当向量彼此比较相似时,改变 WorkingPrecision 的值会得到相差甚远的不同结果.
r = RandomReal[{10 ^ 20, 10 ^ 20 + 1}, {3, 2}, WorkingPrecision -> 30]DistanceMatrix[r] // MatrixFormDistanceMatrix[r, WorkingPrecision -> MachinePrecision] // MatrixForm文本
Wolfram Research (2015),DistanceMatrix,Wolfram 语言函数,https://reference.wolfram.com/language/ref/DistanceMatrix.html (更新于 2017 年).
CMS
Wolfram 语言. 2015. "DistanceMatrix." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2017. https://reference.wolfram.com/language/ref/DistanceMatrix.html.
APA
Wolfram 语言. (2015). DistanceMatrix. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/DistanceMatrix.html 年
BibTeX
@misc{reference.wolfram_2026_distancematrix, author="Wolfram Research", title="{DistanceMatrix}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/DistanceMatrix.html}", note=[Accessed: 04-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_distancematrix, organization={Wolfram Research}, title={DistanceMatrix}, year={2017}, url={https://reference.wolfram.com/language/ref/DistanceMatrix.html}, note=[Accessed: 04-September-2026]}