Question

I have the following script for kmeans in Matlab:

load fisheriris
k = 3; 
clusterIndex = kmeans(meas,3);
scatter(meas(:,1),meas(:,2),[],clusterIndex, 'filled') 

How to plot the centroids of each group? Please help!

Était-ce utile?

La solution

Straight from the docs:

[IDX,C] = kmeans(X,k) returns the k cluster centroid locations in the k-by-p matrix C

So in your case simply do this:

[clusterIndex, centroids] = kmeans(meas,3);

By the way you might like gscatter, it will colour your clusters nicely for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top