function Means = kmeans_means(X, k)
% function y = kmeans(X, k, verbose)
% 
% Finds k cluster in data X

[n,m] = size(X);
max_iter = 1000;

% Initialisieren mit zufaelligen Beispielen
%i = randperm(n);
%Means_old = X(i(1:k),:);

% Initialisieren mit zufaelligen Punkten im Wertebereich der Daten
Xmin = min(X);
Xmax = max(X);
%Means_old = rand(k, m).*repmat(Xmax - Xmin, k, 1) + repmat(Xmin, k, 1);
rndIds = randperm(n);
Means_old = X(rndIds(1:k),:);

for i = 1:max_iter
    R = findMapping(X, Means_old);
    Means = findMeans(X, R);
    if sum(sum((Means_old - Means).^2)) < 1e-8*k*m
        break
    end
    Means_old = Means;
end

Means = Means_old;