function [nearestIdx, coordinates] = myKMeans(vectors, centroids)
%KMeans algoritmus.
%
% vectors - instance/data, ktera se maji shlukovat. (Matice M x N - radky
% jednotlive vektory, sloupce dimenze)
% centroids - pocatecni nastaveni centroidu (Matice K x N)
%
% nearestIdx - vysledne prirazeni vektoru k jednotlivym centroidum
% coordinates - vysledne pozice centroidu (reprezentantu)

numVec = size(vectors,1);

stopping = false;
while(~stopping)

    %Vypocet neblizsiho reprezentanta
    nearestIdx = zeros(numVec, 1);
    for i = 1:numVec
        nearestIdx(i) = nearestCentroid(vectors(i,:), centroids);
    end

    %Presun centroidu (reprezentantu)
    %Tuto cast mate doplnit

    %Mam pokracovat?
    %Tuto cast mate doplnit
    stopping = true;
end

coordinates = centroids;

end


function nearestCentrIdx = nearestCentroid(instance, centroids)
%Vypocet eukleidovske vzdalenosti mezi jednim vektorem a vsemi centroidy
%(reprezentanty).
%
% instance - vektor
% centroids - centroidy
% Vraci index nejblizsiho centroidu

    numCentroids = size(centroids, 1);
    x = repmat(instance, numCentroids, 1);

    dist = x - centroids;
    dist = dist .* dist;

    dist = sqrt(sum(dist,2));

    [tmp, nearestCentrIdx] = min(dist);

end

