kmeans的实现

发布时间:2024-12-05 17:16

增强现实(AR)是虚拟现实与现实世界结合的分支 #生活知识# #科技生活# #虚拟现实技术#

最新推荐文章于 2024-04-24 15:57:52 发布

蝴蝶也可以飞过沧海 于 2014-01-11 20:54:48 发布

function [Y,y]=kmeans(m,k,isRand)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%kMeansCluster-Simple k-means clustering algorithm
%Input:
%m-required,maxtrix data
%k-number of groups
%isRand -optional,if using random initialzation isRand=1,otherwise input
%any number(default),it will assign the first k data as initial centriods.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin<3
    isRand=0;
end
if nargin<2
    k=1;
end

[maxRow,maxCol]=size(m);
Y=zeros(k,maxCol);

if maxRow<=k
    y=[m,1:maxRow]
end

%initial the values of centoid
if isRand
    p=randperm(size(m,1));
    for i=1:k
        c(i,:)=m(p(i),:);
    end
else
    for i=1:k
        c(i,:)=m(i,:);
    end
end

temp=zeros(maxRow,1);  %initial as zero vector

while 1
    d=EuclideanDistance(m,c); %calculate objects-centrid distances
    [z,g]=min(d,[],2); %find group maxtix g
    if g==temp              
        break;         %stop the interation
    else
        temp=g;        %copy graup maxtrix to temporary variable
    end
    for i=1:k
        f=find(g==i);
        if f            %only compute centroid if it is not emmpty
            c(i,:)=mean(m(find(g==i),:),1);
        end
    end
end
Y=c;
y=[m,g];

end

网址:kmeans的实现 https://www.yuejiaxmz.com/news/view/386237

相关内容

机器学习算法实战案例
聚类分析与推荐系统:用户行为分析与个性化推荐1.背景介绍 聚类分析和推荐系统是两个非常重要的领域,它们在现实生活中的应用
艺术与心理治疗的应用:如何利用艺术救助人类心理健康1.背景介绍 艺术和心理治疗在现代社会中的应用逐渐受到了越来越多关注。
智能家居与家庭安全:保护家庭的第一线防线
物联网的应用在家庭监控领域:智能家庭监控与安防
智能家电与家庭健康:提高生活质量的关键因素1.背景介绍 随着科技的发展,智能家电已经成为了人们生活中不可或缺的一部分。智
自动化工作流程的优化与持续改进:如何实现持续的业务效益提升
物联网的应用在家庭监控领域:智能家庭监控与安防1.背景介绍 随着人工智能、大数据和物联网等技术的不断发展,家庭监控领域也
新型家庭智能防盗报警系统设计与实现
自主系统的个性化:为每个用户提供定制化服务1.背景介绍 在当今的数字时代,人工智能和大数据技术已经成为了我们生活和工作的

随便看看