一個(gè)矩陣A的大小為M*N,那么B=pdist(A)得到的矩陣B的大小為1行M*(M-1)/2列,表示的意義是M行數(shù)據(jù),每兩行計(jì)算一下歐式距離(pdist(x,distance),distance也可以用來表示其他距離,默認(rèn)的是歐式距離)。
A = [0 0 0;1 2 3;2 2 2;7 8 9];
dis = pdist(A);%計(jì)算各行向量之間的歐式距離
得到
dis =
3.7417 3.4641 13.9284 1.4142 10.3923 10.4881
squareform(dis)%將向量 dis 轉(zhuǎn)化為矩陣
ans =
0 3.7417 3.4641 13.9284
3.7417 0 1.4142 10.3923
3.4641 1.4142 0 10.4881
13.9284 10.3923 10.4881 0
矩陣中i行 j列元素表示 A中第i個(gè)行向量,與第j個(gè)行向量之間的歐氏距離。
如3.7417為(0 0 0)到(1 2 3)的歐式距離。