MATLAB 帮助中心
格式化距离矩阵
ZOut = squareform(yIn)
yOut = squareform(ZIn)
ZOut = squareform(yIn,'tomatrix')
yOut = squareform(ZIn,'tovector')
ZOut = squareform(yIn) 将 yIn(对于 m 个观测值,它是长度为 m(m–1)/2 的两两距离向量)转换为 ZOut(对角线上全部为零的 m×m 对称矩阵)。
ZOut
yIn
yIn 中的两两距离按 (2,1)、(3,1)、...、(m,1)、(3,2)、...、(m,2)、...、(m,m–1) 的顺序排列。第 i 个和第 j 个观测值之间的两两距离在 ZOut(i,j) 中,如果 i≤j,则两两距离在 yIn((i–1)*(m–i/2)+j–i) 中。
i
j
ZOut(i,j)
yIn((i–1)*(m–i/2)+j–i)
示例
yOut = squareform(ZIn) 将 ZIn(对角线上全部为零的对称方阵)转换为 yOut(对角线下包含 ZIn 元素的向量)。
yOut
ZIn
ZOut = squareform(yIn,'tomatrix') 强制 squareform 将 yIn 视为向量,并将 yIn 转换为矩阵。
squareform
yOut = squareform(ZIn,'tovector') 强制 squareform 将 ZIn 视为矩阵,并将 ZIn 转换为向量。如果 ZIn 是标量 (1×1),则 ZIn 必须为零。
当输入参量是标量时,前两种语法很有用。如果未指定 'tomatrix' 或 'tovector',则默认值为 'tomatrix'。
'tomatrix'
'tovector'
全部折叠
计算成对观测值之间的欧几里德距离,并使用 squareform 将距离向量转换为矩阵。
创建包含三个观测值和两个变量的矩阵。
rng('default') % For reproducibility X = rand(3,2);
计算欧几里德距离。
D = pdist(X)
D = 1×3 0.2954 1.0670 0.9448
两两距离按 (2,1)、(3,1)、(3,2) 顺序排列。通过使用 squareform,您可以轻松定位观测值 i 和 j 之间的距离。
Z = squareform(D)
Z = 3×3 0 0.2954 1.0670 0.2954 0 0.9448 1.0670 0.9448 0
squareform 返回一个对称矩阵,其中 Z(i,j) 对应于观测值 i 和 j 之间的两两距离。例如,您可以找到观测值 2 和 3 之间的距离。
Z(i,j)
Z(2,3)
ans = 0.9448
将 Z 传递给 squareform 函数,以重现 pdist 函数的输出。
Z
pdist
y = squareform(Z)
y = 1×3 0.2954 1.0670 0.9448
squareform 的输出 y 和 pdist 的输出 D 是相同的。
y
D
输入距离向量,指定为长度为 m(m-1)/2 的数值或逻辑向量,其中 m 是观测值数目。
yIn 中的两两距离按 (2,1)、(3,1)、...、(m,1)、(3,2)、...、(m,2)、...、(m,m–1) 顺序排列,即按列向排列 m×m 距离矩阵的左下三角元素。观测值 i 和 j 之间的两两距离对应于 yIn((i–1)*(m–i/2)+j–i),其中 i≤j。
您可以使用 pdist 函数创建 yIn。m 是 pdist 的输入数据中的观测值数目。
数据类型: single | double | logical
single
double
logical
输入距离矩阵,指定为数值或逻辑矩阵。ZIn 是对角线上全部为零的 m×m 对称矩阵,其中 m 是观测值的数目。ZIn(i,j) 表示第 i 个和第 j 个观测值之间的距离。
ZIn(i,j)
距离向量,以长度为 m(m-1)/2 的数值或逻辑向量形式返回,其中 m 是观测值数目。
yOut 中的两两距离按 (2,1)、(3,1)、...、(m,1)、(3,2)、...、(m,2)、...、(m,m–1) 顺序排列,即按列向排列 m×m 距离矩阵的左下三角元素。观测值 i 和 j 之间的两两距离对应于 yOut((i–1)*(m–i/2)+j–i),其中 i≤j。
yOut((i–1)*(m–i/2)+j–i)
yOut 与 pdist 函数的输出具有相同的格式。
距离矩阵,以数值或逻辑矩阵形式返回。ZOut 是对角线上全部为零的 m×m 对称矩阵,其中 m 是观测值的数目。ZOut(i,j) 表示第 i 个和第 j 个观测值之间的距离。
您可以使用 squareform 来格式化类似于距离向量或矩阵的向量或矩阵,例如相关系数矩阵 (corrcoef)。
corrcoef
全部展开
用法说明和限制:转换方向 'tomatrix' 或 'tovector' 必须为编译时常量。例如,要将转换方向指定为 'tovector',请在 codegen (MATLAB Coder) 的 -args 值中包含 coder.Constant('tovector')。
codegen
-args
coder.Constant('tovector')
有关代码生成的详细信息,请参阅 Introduction to Code Generation 和 General Code Generation Workflow。
在 R2006a 之前推出
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处