MATLAB: Running k-means and getting different results run after run

clusteringsilhouette

I am running k-means clustering algorithm on a data, and I don't understand why I am getting different silhouette plots each time I run this. Is there a way to stabilise this? (or set the number of iterations) so I get the same results?

Best Answer

That's normal. Specify 'Replicates' to get convergence.
% Do kmeans clustering on the gray scale image.
grayLevels = double(grayImage(:)); % Convert to column vector.
[clusterIndexes, clusterCenters] = kmeans(grayLevels, numberOfClusters,...
'distance', 'sqEuclidean', ...
'Replicates', 2);
labeledImage = reshape(clusterIndexes, rows, columns);
See attached demo.
Related Question