📄 clustie.m
字号:
function [c,newx,newy]=clustie(x,y,n);
%
% Clustering programme
%
% HELLO!
%
% Determine the maximum and minimum of each
% input variable
%
xmax=max(x);
xmin=min(x);
xmean=mean(x);
range=xmax-xmin;
new_cent=c;
%
% Make some random centres
%
[D L]=size(x);
c=[];
for i = 1 : n
r=rand(1,L);
c(:,i)=(xmin+(r.*range))';
end
for k = 1:50
%
% Find the distances
%
distance=dist(x,c);
%
% Associate each reading with a centre
%
assoc=[];
for i = 1:D
[m assoc(i,1)]=min(distance(i,:));
end
%
% Get the mean value of the x's for each of these clusters
%
ny=[];
for i = 1:n
xdat=[];
ydat=[];
count=1;
for j = 1:D
if assoc(j,1)==i
xdat(count,:)=x(j,:);
ydat(count,:)=y(j,:);
count=count+1;
end
end
if count == 1
new_cent(:,i)=c(:,i);
ny(i,:)=mean(y);
elseif count == 2
new_cent(:,i)=xdat';
ny(i,:)=ydat;
else
new_cent(:,i)=mean(xdat)';
ny(i,:)=mean(ydat);
end
end
%
% Calculate the error
%
er=sum(sum(c-new_cent)./sum(c));
c=new_cent;
new_cent=[];
if abs(er) < 0.0005
break
end
end
newx=c';
newy=ny;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -