pdist
- 文档
- 调用格式:
D = pdist(X)
returns the Euclidean distance between pairs of observations in X.- 一个矩阵A的大小为MN,那么B=pdist(A)得到的矩阵B的大小为1行M(M-1)/2列,表示的意义是M行数据,每两行计算一下欧式距离
pdist(x,distance)
,distance也可以用来表示其他距离,默认的是欧式距离。 - 返回函数中的观测对之间的欧几里得距离
- 一个矩阵A的大小为MN,那么B=pdist(A)得到的矩阵B的大小为1行M(M-1)/2列,表示的意义是M行数据,每两行计算一下欧式距离
D = pdist(X,Distance)
returns the distance by using the method specified by Distance.D = pdist(X,Distance,DistParameter)
returns the distance by using the method specified by Distance and DistParameter. You can specify DistParameter only when Distance is ‘seuclidean’, ‘minkowski’, or ‘mahalanobis’.
- Distance — Distance metric
- Distance metric, specified as a character vector, string scalar, or function handle, as described in the following table.
- metric’取值如下: ‘euclidean’:欧氏距离(默认);
- 栗子:Compute the Euclidean distance between pairs of observations, and convert the distance vector to a matrix using squareform.
- 计算观测对之间的欧氏距离,并用squareform将距离矢量转换为矩阵。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33rng('default') % For reproducibility
X = rand(3,2);
%生成矩阵坐标,第一列为横坐标,第二列为纵坐标
D = pdist(X)
%answer
D = 1×3
0.2954 1.0670 0.9448
%**The pairwise distances are arranged in the order (2,1), (3,1), (3,2)**. **You can easily locate the distance between observations i and j by using squareform.**
%**如果a是3*2矩阵,那么pdist函数计算欧几里德距离的顺序是第二行与第一行计算欧几里得距离,然后是第3行于第一行计算欧几里得距离,最后是第3行与第2行的欧几里德距离**
% 如果`b = squareform(pdist(a))`,那么`b(i,j)`就是矩阵a第i行与第j行之间的欧几里得距离
Z = squareform(D)
Z = 3×3
0 0.2954 1.0670
0.2954 0 0.9448
1.0670 0.9448 0
%squareform returns a symmetric matrix **where Z(i,j) corresponds to the pairwise distance between observations i and j**. For example,** you can find the distance between observations 2 and 3.**
%squareform返回一个对称矩阵,**其中Z(i,j)对应于观察i和j之间的成对距离。**
Z(2,3)
ans = 0.9448
%验证pdist函数计算行顺序
clc,clear;
a = [0 0
3 4
5 6];
b = pdist(a);
d = sqrt(3^2+4^2)
e = sqrt(5^2+6^2)
g = sqrt(2^2+2^2)
b
- 计算观测对之间的欧氏距离,并用squareform将距离矢量转换为矩阵。