% K-MEANS DEMO FOR THE MACHINE LEARNING COURSE.
clear; echo off;

% PARAMETERS:
% ==========

N = 60;              % Number of data.
K = 2;               % Number of clusters.
d = 2;               % Dimension of x.         
max_iterations = 5;

means = 1+randn(K,d);   % Cluster means.
I = zeros(N,K);         % Indicators.

% GENERATE THE DATA:
% =================+

x1 = zeros(N/2,d);
x2 = zeros(N/2,d);
for i=1:N/2,
  x1(i,:) = 2*ones(1,d) + 0.5*randn(1,d);
  x2(i,:) = 0*ones(1,d) + 0.5*randn(1,d);
end;  
x=[x1; x2];   % Data generated from two clusters.

% Plot the data:
figure(1)
clf;
plot(x(:,1),x(:,2),'go',means(:,1),means(:,2),'k+','linewidth',2)
pause;

 
% TRAIN K-MEANS:
% ==============



for i = 1:max_iterations,
  
  % Compute the assignments:
  I = zeros(N,K);  
  for j=1:N,
    distances = sqrt(sum(((repmat(x(j,:),K,1) - means).^2)'));    
    c = find(distances == min(distances));
    I(j,c) = 1;
  end;  

  % Plot assignments:
  figure(1)
  hold on;
  for j=1:N,
    for c=1:K,
      if I(j,c) == 1
        plot(x(j,1),x(j,2),'ro','linewidth',2);
      else
        plot(x(j,1),x(j,2),'bo','linewidth',2);
      end;
    end;
  end;  
  pause;
  % Delete old means from plot:
  figure(1)
  plot(means(:,1),means(:,2),'w+','linewidth',2)
   
  % Compute new means:
  for c=1:K,
    means(c,:) = zeros(1,d);
    for j=1:N,
      means(c,:) = means(c,:)+I(j,c)*x(j,:);
    end;
    means(c,:) = means(c,:)./sum(I(:,c));   
  end;
  
  % Plot new means:
  figure(1)
  plot(means(:,1),means(:,2),'k+','linewidth',2)
  pause;
 
end;  
