MATLAB implements the kmeans algorithm ..
% Random acquisition of 150 points x = [randn (50, 2) + ones (50, 2); randn (50, 2)-ones (50, 2); randn (50, 2) + [ones (50, 1 ), -ones (50,1)]; opts = statset ('display', 'final '); % call the kmeans function % x n * P data matrix % idx N * 1 vector, storing the matrix of the clustering label % CTRs K * P for each vertex, stores the sum of % sumd 1 * K and vectors of K cluster center locations, and stores the matrix of % d N * K of the distance between all points of the class and the center of the class, the distance between each vertex and all centers is stored. [idx, CTRs, sumd, d] = kmeans (x, 3, 'replicates ', 3, 'options', opts ); % plot the point with clustering 1. X (idx =), which is the first coordinate of the first class of samples; X (idx =) it is the second coordinate of the second type of sample, plot (x (idx =), x (idx =), 'R. ', 'markersize', 14) Hold onplot (x (idx = 2, 1), x (idx = 2, 2),' B. ', 'markersize', 14) Hold onplot (x (idx = 3, 1), x (idx = 3, 2), 'G. ', 'markersize', 14) % plot the cluster center. kx indicates Circular plot (CTRs (:, 1), CTRs (:, 2), 'kx ', 'markersize', 14, 'linewidth', 4) plot (CTRs (:, 1), CTRs (:, 2), 'kx', 'markersize', 14, 'linewidth', 4) plot (CTRs (:, 1), CTRs (:, 2), 'kx', 'markersize', 14, 'linewidth', 4) legend ('Cluster 1', 'Cluster 2', 'Cluster 3', 'centroids ', 'location', 'nw') ctrssumd
Result:
Address: http://blog.sina.com.cn/s/blog_62186b46010145ne.html