一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【聚类分析】基于matlab GUI K-means聚类分析【含Matlab源码 791期】

获取代码方式2:
通过紫极神光博客主页开通CSDN会员,凭支付凭证,私信博主,可获得此代码。

获取代码方式3:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:开通CSDN会员,仅只能免费获得1份代码(有效期为开通日起,三天内有效);
订阅紫极神光博客付费专栏,可免费获得2份代码(有效期为订阅日起,三天内有效);

二、部分源代码

function KmeansMainclose all;clear;clc;%随机生成随机数mu = [0 0];%协方差矩阵,对角为方差值0.3,0.35var = [0.3 0; 0 0.35];samNum = 200;data = mvnrnd(mu, var, samNum);a = figure;plot(gca, data(:,1), data(:,2), '*', 'color', 'k');hold on;classNum = [];%类数iterNum = 0;%迭代次数x = [];centerPoint = [];centerPointPathAarry = [];h_plotCenterPoint = [];%中心点绘制handleh_plotPath = [];%中心点路径绘制handle%centerPointPathAarry结构%第1次迭代|中心点1(x,y)|中心点2(x,y)|中心点3(x,y)|中心点n(x,y)%第2次迭代|中心点1(x,y)|中心点2(x,y)|中心点3(x,y)|中心点n(x,y)h_slider = uicontrol(a,'Style', 'slider',...'SliderStep',[0.02 0.02],...'Min',0,'Max',50,'Value',0,...'Position', [400 20 100 20],...'Callback', {@classify,gca});   h_edit = uicontrol(a,'Style', 'edit',...  'String', '200',...'Position', [80 20 40 20],...'Callback', {@paintRandomPoint,gca});   uicontrol('Style', 'popup',...'String', '自定义|随机2点|随机3点|随机4点',...'Position', [200 22 120 20],...'Callback', {@SpsfPoit,gca});   h_t1 = uicontrol('Style','text','String','迭代', ...'Position', [355 20 40 20]);h_textClassNum = uicontrol('Style','text','String','中心点', ...'Position', [140 20 55 20]);uicontrol('Style','text','String','样本点数:', ...'Position', [25 20 50 20]);h_textshow = uicontrol('Style','text','String','0','Position', [500 20 20 20]);                         set(gca,'xtick',[],'ytick',[],...'title',text('string','Kmeans演示脚本','color','k'));xlim([-1.5 1.5]);ylim([-1.5 1.5]);%%%%%%%%%%%%%%%%%%%%function SpsfPoit(hObj,event,ax)set(h_slider,'value',0);    %清零滑动条,以实现从0迭代cla;%清空axesset(h_textshow,'string',0);%界面显示的迭代次数清零%句柄赋值为空h_plotCenterPoint = [];h_plotPath = [];centerPointPathAarry = [];%轨迹归零plot(gca, data(:,1), data(:,2), '*', 'color', 'k');%样本点颜色初始化val = get(hObj, 'Value');%获得popup menu的值if val == 1   %选择任意若干点作为中心点[x, y] = ginput;centerPoint = [x y];[classNum, ~] = size(centerPoint);repaintBeginPoint(h_plotCenterPoint, classNum, centerPoint);elseif val == 2%选择任意2点作为中心点centerPoint = rand(2, 2)*2-0.5;[classNum, ~] = size(centerPoint);repaintBeginPoint(h_plotCenterPoint, classNum, centerPoint);elseif val == 3%选择任意3点作为中心点centerPoint = rand(3, 2)*2-0.5;[classNum, ~] = size(centerPoint); repaintBeginPoint(h_plotCenterPoint, classNum, centerPoint);elseif val == 4%选择任意4点作为中心点centerPoint = rand(4,2)*2-0.5;[classNum,~] = size(centerPoint);repaintBeginPoint(h_plotCenterPoint, classNum, centerPoint);end[labelSample] = classifyAndShowAndLabel(classNum, centerPoint, data, samNum, gca);centerPointPathAarry = [centerPointPathAarry; reshape(centerPoint', 1, classNum*2)];set(h_textClassNum, 'string', [num2str(classNum) '个中心点']);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%迭代分类函数%%%%%%%%%%%%%%%%%%%%function classify(hObj,event,ax)iterNum = round(get(hObj, 'value')); set(h_textshow, 'string', iterNum);%根据起始点分类,并且为不同的类标记不同颜色,返回带标签样本数据[labelSample] = classifyAndShowAndLabel(classNum, centerPoint, data, samNum, gca);%重新获得起始点矩阵centerPoint(x|y)[centerPoint] = recalClassCenter(labelSample, classNum);centerPointPathAarry = [centerPointPathAarry; reshape(centerPoint', 1, classNum*2)];%重新绘制起始点centerPoint(x|y)到axes上repaintBeginPoint(h_plotCenterPoint, classNum, centerPoint);disp('path:');disp(centerPointPathAarry);%将中心点的轨迹显示出来for i = 1:classNum[selected_color] = colorMap(i, classNum);h_plotPath(i)=plot(centerPointPathAarry(:, (i*2)-1), centerPointPathAarry(:,i*2), 'color',  selected_color);endend%%%%%%%%%%%%%%%函数部分%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%重新绘制起始点函数%%%%%%%%%function repaintBeginPoint(handle_plo,classnum,R)delete(h_plotCenterPoint);%清除绘制的中心点,并将句柄赋值为空h_plotCenterPoint=[];%重新绘制起始点,每个起始点的颜色不同for i = 1:classnum[selected_color] = colorMap(i, classnum);h_plotCenterPoint(i) = plot(R(i,1), R(i,2), 'o', 'MarkerSize', 7, 'MarkerFaceColor', selected_color); endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%重新计算类重心%%%%%%%%%%%%%%%%%%%%%%function [newCenterPoint]=recalClassCenter(labelSample,classNum)%R为重新被计算的类中心newCenterPoint=[];%分类并且计算每个类的重心for i=1:classNum      %取出所有标签为i类的所有行,即第i类的所有点classs=labelSample(labelSample(:,3)==i,:);%有用的只有第一列和第二列,去除标签列classs=[classs(:,1),classs(:,2)];%重新计算重心classs_repoint=mean(classs);newCenterPoint=[newCenterPoint;classs_repoint];endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%根据起始点分类,并且为不同的类标记不同颜色,返回带标签样本数据%%%%%%%%%function [labelSample]=classifyAndShowAndLabel(classNum,centerPoint,data1,samNum,gca)  disArray=[];for i=1:classNumcalproA=[centerPoint(i,:);data1(:,1),data1(:,2)];Adist=pdist(calproA,'euclidean');Adist=Adist(1:samNum)';disArray=[disArray,Adist];end%拼接,得到距离矩阵,一列代表一个点到所有样本点的距离%disArray=[Adist Bdist];%disp(disArray);%获取每一行最小值所在距离矩阵的列%并和原样本矩阵拼接为labelSample%labelSample 表示被标记的原始样本,每一行为一个样本%每一行的最后一列为标记值,在这里标记是距离哪个样本点最近。minn=min(disArray');cols=[];for i=1:length(minn)[row,col] = find(disArray==minn(i));cols(i)=col;endcols=cols';labelSample=[data1(:,1),data1(:,2),cols];%将不同类的点标上不同的颜色for i=1:samNum[selected_color]=colorMap(labelSample(i,3),classNum);plot(gca,data1(i,1),data1(i,2),'*','color',selected_color);endend

三、运行结果

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 由伟,刘亚秀.MATLAB数据分析教程[M].清华大学出版社,2020.
[2]王岩,隋思涟.试验设计与MATLAB数据分析[M].清华大学出版社,2012.

【聚类分析】基于matlab GUI K-means聚类分析【含Matlab源码 791期】相关推荐

  1. 【光学】基于matlab GUI杨氏双缝干涉【含Matlab源码 001期】

    一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[光学]基于matlab GUI杨氏双缝干涉[含Matlab源码 001期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: 付费 ...

  2. 【人脸识别】基于matlab GUI人数统计【含Matlab源码 2121期】

    ⛄一.人数统计简介(附课程作业报告) 1 课题背景 本课题为基于matlab的人数统计系统.近年来,很多行业对人流信息有极大的需求,如汽车公交站,地铁站台,商场出入口等.通过人数统计系统可以方便.可靠 ...

  3. 基于SSM的仓库管理系统(含完整源码+论文)

    后端框架:SSM 数据库:MySQL 开发工具:IDEA/Eclipse 系统介绍:本系统是基于SSM框架进行设计,MySQL作为底层数据库,前端采用bootstrap 模块大致介绍:包括库存管理.出 ...

  4. 【条形码识别】基于matlab GUI二维条形码识别【含Matlab源码 607期】

    ⛄一.二维条形码识别简介 采用数字图像处理技术对二维条码进行识别, 是二维条形码识别中较常用的方法, 它在处理污损的条形码方面有光电识别方法无法比拟的优势.另外图像式识别方法对识别角度的要求没有光电识 ...

  5. 【心电信号】基于matlab GUI心电信号预处理【含Matlab源码 938期】

    ⛄一.心电信号预处理方法简介 理论知识参考文献:心电信号预处理方法研究 ⛄二.部分源代码 function varargout = kaishi(varargin) gui_Singleton = 1 ...

  6. matlab svm 语音识别,【情感识别】基于matlab支持向量机(SVM)的语音情感识别【含Matlab源码 543期】...

    一.简介 支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本.非线性及高维模式识别中表现出许多特有的优势,并能够推广应用到函数 ...

  7. 【数据分析】基于matlab GUI齿轮箱振动数据分析【含Matlab源码 2122期】

    一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[数据分析]基于matlab GUI齿轮箱振动数据分析[含Matlab源码 2122期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方 ...

  8. 【数字信号】基于matlab GUI多音双频(DTMF)拨号音频解码仿真系统【含Matlab源码 1084期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[数字信号]基于matlab GUI多音双频(DTMF)拨号音频解码仿真系统[含Matlab源码 1084期] 点击上面蓝色字体,直接付费 ...

  9. 【图像处理】基于matlab GUI多功能图像处理系统【含Matlab源码 1876期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[图像处理]基于matlab GUI多功能图像处理系统[含Matlab源码 1876期] 点击上面蓝色字体,直接付费下载,即可. 获取代码 ...

  10. 【图像去噪】基于matlab GUI butterworth+中值+维纳+小波图像去噪【含Matlab源码 520期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[图像去噪]基于matlab GUI butterworth+中值+维纳+小波图像去噪[含Matlab源码 520期] 获取代码方式2: ...

最新文章

  1. 高斯赛德尔迭代c语言_逐次超松弛SOR迭代概述
  2. AngularJs Cookie 的使用
  3. BZOJ 2733 | 洛谷 P3224 [HNOI2012]永无乡
  4. 今天第一次写博客,先不说技术了
  5. Visual Studio 2017 RC3支持.NET Core,延迟对Python的支持
  6. webresource_Jersey WebResource –标头不附加
  7. C#.Net工作笔记005---c#中list合并去重_以及单纯合并_值类型list去重
  8. linux git 搭建 debian,基于Debian Linux搭建Git服务器
  9. 中文怎么编码成计算机,如何将汉字转换成二进制编码
  10. 浅析Java的线程和Golang的协程
  11. 支付宝生活号开发中所遇到的困难及解决记录
  12. 归一化互相关(NCC)及其部分应用场景
  13. linux下route路由设置命令详解
  14. 新浪微博小工具--PC遥控器1.0发布
  15. 【AGC012E】 Camel and Oases ST表+状压dp
  16. 入门铺路——python
  17. 选择所在城市html按字母,微信小程序实现根据字母选择城市功能
  18. 如何在网络上找到另一台计算机,怎么用一个电脑通过另一台电脑共享上网?
  19. 【SIMCOM A7670C】Android8.1 4G Dongle 移植笔记
  20. 杂项-公司:软银银行集团

热门文章

  1. 标准C++ 与 VC++ 区别集锦(待续)
  2. Improving Opencv10 More Morphology Transformations
  3. 20200602每日一句
  4. 20200123每日一句
  5. 20190910每日一句 你有勇气直面自己的恐惧吗?
  6. 190615每日一句;每个学生都不能错过的9个人生忠告; 什么样心态的人,才能取得最终的成功
  7. 181230每日一句
  8. AR/MR研究团队和机构
  9. atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php
  10. paip.网站提示SESSION过期 登录过期 以及二次登录的问题