function col_labels= my_kmeans(img, M, k)
img = double(img);
% M must be k * nfeat

l = length(img);
labels = zeros(l,1);

labels_old = ones(l,1);

dis = zeros(l,k);
while sum(abs(labels-labels_old)) >0
    labels_old = labels;
    
    for i=1:k
        m = repmat(M(i,:),[l,1]);
        
        dis(:,i) = sqrt(sum((m-img).^2 ,2));
        
    end
    
    [minni,labels] = min(dis,[],2);
    
    
    %aggiorno le medie
     for i=1:k
         M(i,:) = mean(img(labels==i,:),1);
         if isnan(M(i,1))
             M(i,:) = rand(1,3)*255;
         end
     end
     M
    
end

col_labels = zeros(l,3);

for i=1:k
    for j=1:l
        if labels(j)==i
            col_labels(j,:) = M(i,:);
        end
    end
end
col_labels=uint8(col_labels);

