% 创建数据矩阵
data = [
0 0.71 0 5.45 1.51 4.78 5.75 0 0 0;
0 0 0.47 1.59 0 8.46 0.14 0.91 0 0;
0 1.41 4.49 4.35 2.12 0 0 0.35 0.4 0;
0 0 4.24 3.86 2.74 0 1.41 0.48 0.44 0;
0 0 1.6 3.11 4.59 0.44 1.62 0.3 0 0;
0 0.21 0.46 2.36 1 0.11 0.17 0 0 0;
0 0.15 0.64 2.35 0 0.47 0.13 0 0 0;
0 0 0.38 1.44 0.17 0.16 0.42 0 0 0;
0 0.23 0.89 2.72 0 3.01 1.46 0.31 0 3.66;
2.66 0.11 0.84 5 0 0.53 0 0.23 0 0;
0 0.25 0 4.79 0 0.77 0.2 0.43 0 0;
4.66 0.29 0.87 3.06 0 0.65 0.1 0.85 0 0;
2.71 0 1.13 1.45 0 0.86 0.35 0 0 0;
0 0.3 2.08 6.5 1.27 0.45 4.32 0.3 0 0;
0 0 3.12 4.16 0 0.7 6.34 0.23 0 0;
3.04 0.11 0.78 6.06 0 0.54 0 0.27 0 0;
3.06 0.2 2.14 12.69 0.77 0.43 0 0.26 0 0;
5.68 0.35 0 5.66 0 2.72 0 0 0 0;
5.74 0.15 0.79 3.53 0 2.67 0.08 0.35 0 0;
0.92 0.3 2.98 14.34 0.81 0.74 0.41 0.25 0 0;
0 0.26 1.34 4.7 0.41 0.33 1.04 0.12 0.23 0;
2.31 0 0.63 1.9 1.55 1.12 0.19 0.2 0 0;
7.92 0 0.5 1.42 0 2.99 0 0.33 0 0;
];
% 计算距离矩阵
distMatrix = pdist(data, 'euclidean');
% 使用重心法进行分层聚类
Z = linkage(distMatrix, 'centroid');
% 绘制树状图
figure;
dendrogram(Z);
title('Hierarchical Clustering Dendrogram (Centroid Method)');
xlabel('Sample Index');
ylabel('Distance');
% 根据树状图确定聚类数,这里假设分成4类
numClusters = 4;
clusters = cluster(Z, 'maxclust', numClusters);
% 显示每个样本的聚类分配
disp('Cluster assignments:');
disp(clusters);
% 进行主成分分析
[coeff, score] = pca(data);
% 绘制前两个主成分的散点图
figure;
gscatter(score(:,1), score(:,2), clusters);
title('PCA of Clustered Data');
xlabel('First Principal Component');
ylabel('Second Principal Component');
基于重心法的matlab分层聚类
最新推荐文章于 2024-10-06 15:48:05 发布