matlab画聚类图
时间: 2023-08-05 15:09:34 浏览: 90
matlab图像聚类
在Matlab中,可以使用kmeans函数进行聚类,并使用gscatter函数绘制聚类图。以下是一个示例代码:
```matlab
% 随机生成一组数据
rng(1)
PntSet1 = mvnrnd(\[2 3\], \[1 0; 0 2\], 500);
PntSet2 = mvnrnd(\[6 7\], \[1 0; 0 2\], 500);
PntSet3 = mvnrnd(\[6 2\], \[1 0; 0 1\], 500);
X = \[PntSet1; PntSet2; PntSet3\];
% kmeans聚类
K = 3;
\[idx, C\] = kmeans(X, K);
% 配色
colorList = \[0.4 0.76 0.65; 0.99 0.55 0.38; 0.55 0.63 0.80\];
% 绘制散点图
figure()
hold on
for i = 1:K
scatter(X(idx==i, 1), X(idx==i, 2), 80, 'filled', ...
'LineWidth', 1, 'MarkerEdgeColor', \[1 1 1\]*.3, 'MarkerFaceColor', colorList(i, :));
end
% 坐标区域修饰
ax = gca;
ax.LineWidth = 1.4;
ax.Box = 'on';
ax.TickDir = 'in';
ax.XMinorTick = 'on';
ax.YMinorTick = 'on';
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.GridLineStyle = '--';
ax.XColor = \[.3, .3, .3\];
ax.YColor = \[.3, .3, .3\];
ax.FontWeight = 'bold';
ax.FontName = 'Cambria';
ax.FontSize = 11;
% 图例
legend('Cluster 1', 'Cluster 2', 'Cluster 3')
% 图片输出
figW = 800; % 图片宽度
figH = 600; % 图片高度
set(gcf, 'PaperUnits', 'points');
set(gcf, 'PaperPosition', \[0 0 figW figH\]);
print('cluster_plot.png', '-r300', '-dpng');
```
这段代码会生成一个散点图,其中不同颜色的点表示不同的聚类簇。你可以根据需要调整代码中的参数,如数据生成方式、聚类数目、配色方案等,以满足你的需求。最后,代码会将绘制的图像保存为名为"cluster_plot.png"的PNG文件。
#### 引用[.reference_title]
- *1* [MATLAB | kmeans聚类如何绘制更强的聚类边界(决策边界)](https://blog.csdn.net/slandarer/article/details/127071211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [MATLAB 按组聚类绘图gscatter](https://blog.csdn.net/qq_43108090/article/details/123957360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Matlab进阶绘图第8期—聚类/分类散点图](https://blog.csdn.net/qq_26447137/article/details/129612483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文