Implement kmeans using Matlab

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.