%kmeans.m
%kmeans algorithm 
%Generation of data 
C1=[0.7 0.3;0.2 0.9] 
[E1,D1]=eig(C1); 
M1=rand(1,2)+[2 2]; 
C2=[0.9 0.1;0.4 0.6]; 
[E2,D2]=eig(C2); 
M2=rand(1,2)+[-2 -2]; 
%The prior probability of the first Mixture and the second Mixtures are %0.2 and 0.8 respectively. 
%Generation of 10000 data with additive noise from constructed Model is as %given below 
figure(1) 
DATA=[]; 
INDEX=[];
for l=1:1:200
r=rand; 
if(r<0.2)
    temp=randn(2,1);
    temp1=E1'*D1.^(1/2)*temp+M1; 
    hold on
    plot(temp1(1),temp1(2),'ro');
    %second model 
    DATA=[DATA temp1];
    INDEX=[INDEX 1];
else
    %first model 
    temp=randn(2,1);
    temp2=E2'*D2.^(1/2)*temp+M2;
    hold on 
    plot(temp2(1),temp2(2),'bo');
    DATA=[DATA temp2];
    INDEX=[INDEX 2];
end
end
%DATA is clustered using kmeans algorithm as given below. 
%Initialize mean 
m1=rand(2,1); m2=rand(2,1); M{1}=[m1 m2];
for iter=2:1:100 
    for i=1:1:length(DATA)
        [P1]=sum((DATA-repmat(m1,1,400)).^2) ;
        [P2]=sum((DATA-repmat(m2,1,400)).^2) ;
    end
    P=[P1;P2];
    [U,V]=min(P);
    [x1,y1]=find(V==1);
    [x2,y2]=find(V==2); 
    m1=mean(DATA(:,y1)')';
    m2=mean(DATA(:,y2)')';
    temp1=DATA(:,V(y1));
    temp2=DATA(:,V(y2));
    TEMP1{iter}=temp1; 
    TEMP2{iter}=temp2;
    M{iter}=[m1 m2];
end
t=[1 2 3];
for i=1:1:3 
    subplot(1,3,i)
    plot(M{t(i)}(1,1),M{t(i)}(2,1),'ro')
    hold on
    plot(M1(1,1),M1(1,2),'r*')
    hold on
    plot(M{t(i)}(1,2),M{t(i)}(2,2),'bo')
    hold on
    plot(M2(1,1),M2(1,2),'b*')
end
