function kmeans_test
% k-means test program
% Lecture: Unsupervised Learning (C.E.Rasmussen & M.O.Franz)
% 
% creates a 3 cluster point cloud and runs k-means or soft k-means. Stops
% after each iteration and waits for user.
%
% Copyright Matthias Franz and Carl Edward Rasmussen 2006-04-29.


% constants
clmeans = [[1, 1]; [-1, 1]; [0, -1];]; % cluster means
n = 100; % number of data points per cluster
max_sigma = 0.8; % maximal sigma
k = 3; % number of means
beta = 3;

% create data cloud of m Gaussian clusters
x = [];
for i=1:3 % create 3 clusters
    sigma = max_sigma*rand(2,1); % random covariance in x and y direction
    cluster(:, 1) = repmat(clmeans(i, 1), n, 1) + sigma(1)*randn(n, 1); % x-component
    cluster(:, 2) = repmat(clmeans(i, 2), n, 1) + sigma(2)*randn(n, 1); % y-component
    x = [x; cluster]; % append cluster to data cloud
end    

% k-means initialisation
m = rand(k, 2); % new means
m_old = zeros(k, 2); % old means

% k-means iterations
while m_old ~= m % iterate while means change
    m_old = m;
    
    % plot data and means
    plot(x(:,1), x(:,2), '. b')
    hold on
    plot(m(:,1), m(:,2), 'o r');
    hold off
    input('press return')
    
    % compute new means
    %m = kmeans(x, m_old);
    m = soft_kmeans(x, m_old, beta);
end
disp(m)

