%
echo on
format short
format compact

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MATLAB K-MEANS CLUSTERING DEMO
%
% NOTE that when "pause" occurs in the file,
%      you'll need to hit the space bar (or other key)
%      to move the demo forwards.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  LIKE NOW:

pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Generate random data for k-means examples:
%    100 points from each of 3 multivariate Gaussians:

N = 100;

mu1 = [50 100];  sigma1 = [40 10; 10 70];
D1 = mvnrnd(mu1,sigma1,N);

mu2 = [100 50]; sigma2 = [50 0; 0 40];
D2 = mvnrnd(mu2,sigma2,N);

mu3 = [70 70]; sigma3 = [50 20; 20 30];
D3 = mvnrnd(mu3,sigma3,N);

% Combine into one matrix, and permute the rows randomly

Dall = [D1;D2;D3]; Dp = randperm(3*N); Dall1 = Dall(Dp,:);

% NOTE that after the pause there will two figures to look at...
pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Calculate range (for plotting)

rr = [min(min(Dall)) max(max(Dall))];

% Plot the three random subsets separately:

figure(1);
hold off; plot(D1(:,1),D1(:,2),'bx', D2(:,1),D2(:,2), 'ro', D3(:,1),D3(:,2),'g+')
xlim(rr); ylim(rr);

% Plot the whole artificial dataset at once:

figure(2)

plot(Dall1(:,1),Dall1(:,2),'go')

pause

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Divide (randomized) matrix into three arbitrary categories, rowwise:
Cats = [ones(N,1); 2*ones(N,1); 3*ones(N,1)];

LastMM = [0 0; 0 0; 0 0];   % initialization

%%%%% ITERATION %%%%%

for rep=1:20

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Find centroids of current three classes:

MM = [mean(Dall1(Cats==1,:)); mean(Dall1(Cats==2,:)); mean(Dall1(Cats==3,:))]

% Assign all data points to nearest centroid:

echo off
for n=1:(3*N)
    for m=1:3
        x(m,:) = Dall1(n,:)-MM(m,:); dist(m) = sqrt(x(m,:)*x(m,:)');
    end
    [val ind] = min(dist);
    Cats(n) = ind;
end
echo on

% Plot the centroids and the assignments of points:
figure(3)
hold off
plot(Dall1(Cats==1,1), Dall1(Cats==1,2), 'bx', ...
     Dall1(Cats==2,1), Dall1(Cats==2,2), 'g+', ...
     Dall1(Cats==3,1), Dall1(Cats==3,2), 'ro');
hold on
plot(MM(1,1),MM(1,2), 'bs', 'MarkerSize', 20, 'MarkerFaceColor', 'b')
plot(MM(2,1),MM(2,2), 'gs', 'MarkerSize', 20, 'MarkerFaceColor', 'g')
plot(MM(3,1),MM(3,2), 'rs', 'MarkerSize', 20, 'MarkerFaceColor', 'r')

title(['Iteration No. ',int2str(rep)])

% Iterate unless criterion has been reached...

mdiff = sum(sum((LastMM-MM).^2))
if (mdiff < realmin), break, end

LastMM = MM;
pause
end




