Matlab kmeans
时间: 2023-12-24 07:51:51 浏览: 102
K-means clustering is a popular clustering algorithm used in data science and machine learning. It is a type of unsupervised learning where the algorithm tries to group similar data points together.
In Matlab, the kmeans function is used to perform k-means clustering. The function takes in a dataset and the number of clusters required as input. The output of the function is the cluster indices for each data point and the centroids of each cluster.
Here is an example code for performing k-means clustering in Matlab:
```matlab
% Load data
load fisheriris
% Perform k-means clustering
[idx, C] = kmeans(meas, 3);
% Plot the clusters
gscatter(meas(:,1), meas(:,2), idx)
hold on
plot(C(:,1), C(:,2), 'kx', 'MarkerSize', 15, 'LineWidth', 3)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids','Location','NW')
```
In this example, the fisheriris dataset is loaded and k-means clustering is performed with 3 clusters. The indices of the clusters and the centroids are stored in the 'idx' and 'C' variables respectively. Finally, the clusters are plotted using the 'gscatter' function and the centroids are plotted using the 'plot' function.
阅读全文