类内平均距离matlab,模式识别matlab编程:用k次平均法将20个样本分成2类
编程题.已知有20个样本,每个样本有2个特征,数据分布如下图:MA TLAB程序代码:clearx=[0 0;1 0;0 1; 1 1;2 1;1 2;2 2;3 2;6 6;7 6;8 6;6 7;7 7;8 7;9 7;7 8;8 8;9 8;8 9; 9 9];z=zeros(2,2);z1=zeros(2,2);z=x(1:2,1:2);while 1count=zeros(2,1);al
编程题.
已知有20个样本,每个样本有2个特征,数据分布如下图:
MA TLAB程序代码:
clear
x=[0 0;1 0;0 1; 1 1;2 1;1 2;2 2;3 2;6 6;7 6;8 6;6 7;7 7;8 7;9 7;7 8;8 8;9 8;8 9; 9 9];
z=zeros(2,2);
z1=zeros(2,2);
z=x(1:2,1:2);
while 1
count=zeros(2,1);
allsum=zeros(2,2);
for i=1:20 % 对每一个样本i,计算到2个聚类中心的距离
temp1=sqrt((z(1,1)-x(i,1)).^2+(z(1,2)-x(i,2)).^2);
temp2=sqrt((z(2,1)-x(i,1)).^2+(z(2,2)-x(i,2)).^2);
if (temp1count(1)=count(1)+1;
allsum(1,1)=allsum(1,1)+x(i,1);
allsum(1,2)=allsum(1,2)+x(i,2);
else
count(2)=count(2)+1; %统计属于第2类的样本数量
allsum(2,1)=allsum(2,1)+x(i,1);
allsum(2,2)=allsum(2,2)+x(i,2);
end
end
z1(1,1)=allsum(1,1)/count(1); % 计算各个聚类的新向量值
z1(1,2)=allsum(1,2)/count(1);
z1(2,1)=allsum(2,1)/count(2);
z1(2,2)=allsum(2,2)/count(2);
更多推荐
所有评论(0)