function cluster = kmeans(M, K, cent) doLoop = 1; while(doLoop) W = cent; cluster = zeros(1); for i=1:size(M,1) minval = 1000; for j=1:K temp = norm(M(i,:)-W(j,:)); if(temp < minval) minval = temp; cluster(i) = j; end end end for j=1:K cent(j,:) = mean(M(find(cluster == j),:)); end %Check if we should continue the whileloop if(sum(W == cent) == K) doLoop = 0; end end