Can anyone comment ( role of each line) this k-means matlab code ?

1 view (last 30 days)
% Data Generation
if true
% code
end
m1=[2 0];
m2=[-2 0];
Close all
N=100;
x=[];S=[];
for i=1:N
r=randn;
if r>0
x=[x;m1+randn(1,2)]
S=[S;1]
else
x=[x;m2+randn(1,2)]
S=[S;2]
end
end
ii1=find(S==1);
ii2=find(S==2);
plot(x(ii1,1),x(ii1,2),'o',x(ii2,1),x(ii2,2),'x')
%%K-means
mean1=[0,1];
mean2=[0,0];
m1=mean1+1;
m2=mean2+1;
k=0
while((mean1~=m1)&(mean2~=m2))
k=k+1
m1=mean1;
m2=mean2;
for i=1:length(x)
e1=x(i,:)-mean1
e2=x(i,:)-mean2
d(1)=e1*e1'
d(2)=e2*e2'
c(i)=find(d==min(d))
end
il1=find(c==1)
il2=find(c==2)
mean1=mean(x(il1,:))
mean2=mean(x(il2,:))
end
figure(2)
plot(x(il1,1),x(il1,2),'o',x(il2,1),x(il2,2),'x')
hold on
plot(mean1(1,1),mean1(1,2),'ro',mean2(1,1),mean2(1,2),'r+')

Answers (1)

Walter Roberson
Walter Roberson on 2 Feb 2014

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!