简体   繁体   中英

How to perform k-means algorithm in MATLAB?

I'm new to matlab and I want to know how to perform k-means algorithm in MATLAB, and also I want to know how to define cluster centers when performing k means.

For example, let's say I'm creating an array as given below.

m = [11.00; 10.60; 11.00; 12.00; 11.75; 11.55; 11.20; 12.10; 11.60; 11.40;...
     11.10; 11.60; 11.00; 10.15; 9.55; 9.35; 8.55; 7.65; 7.80; 8.45; 8.05]

I want to cluster the above values into 5 clusters where k = 5.

And I would like to take the cluster centers as 2, 5, 10, 20, 40.

So my question is how can I define the cluster centers and perform k-means algorithm in MATLAB? Is there a specific parameter to set the cluster centers in MATLAB kmeans() function?

Please help me to solve the above problems.

The goal of k-means clustering is to find the k cluster centers to minimize the overall distance of all points from their respective cluster centers.

With this goal, you'd write

[clusterIndex, clusterCenters] = kmeans(m,5,'start',[2;5;10;20;40])

This would adjust the cluster centers from their start position until an optimal position and assignment had been found.

If you instead want to associate your points in m to fixed cluster centers, you would not use kmeans , but calculate the clusterIndex directly using min

distanceToCenter = bsxfun(@minus,m,[2 5 10 20 40]);
[~, clusterIndex] = min(abs(distanceToCenter),[],2); 

ie. you calculate the difference between every point and every center, and find, for each point, the center with the minimum (absolute) distance.


To plot results, you can align the points on a line and color them according to the center:

nCenters = length(clusterCenters);
cmap = hsv(nCenters);
figure
hold on
for iCenter = 1:nCenters
    plot(m(iCenter==clusterIndex),1,'.','color',cmap(iCenter,:));
    plot(clusterCenters(iCenter),1,'*','color',cmap(iCenter,:));
end

You can see a classic implementation in my post here - K-Means Algorithm with Arbitrary Distance Function Matlab (Chebyshev Distance) .

Enjoy...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM