导入现有二维经纬度坐标的excel,对其进行K-means聚类的matlab可视化代码,设定聚类点为8个,并且得出 每个中心聚类点的坐标,最后将每个聚类区域所包含的坐标的Excel编号导出到单独的Excel文件中。
时间: 2024-06-11 20:07:39 浏览: 193
以下是一个可能的解决方案:
导入经纬度坐标的Excel文件,例如名为“coordinates.xlsx”的文件。其中,第一列是Excel编号,第二列是经度,第三列是纬度。
data = xlsread('coordinates.xlsx'); coordinates = data(:,2:3);
运用K-means算法对坐标进行聚类,设定聚类点为8个。
k = 8; [idx, centers] = kmeans(coordinates, k);
可视化聚类结果,用不同颜色表示不同聚类区域。
colors = ['r','g','b','c','m','y','k','w']; figure; hold on; for i = 1:k scatter(coordinates(idx==i,1), coordinates(idx==i,2), 5, colors(i), 'filled'); end scatter(centers(:,1), centers(:,2), 50, 'k', 'filled', 'Marker','o','LineWidth',2); title('K-means Clustering'); xlabel('Longitude'); ylabel('Latitude');
输出每个中心聚类点的坐标,并保存到Excel文件中。
center_excel = [1:k; centers']; xlswrite('centers.xlsx', center_excel);
将每个聚类区域所包含的坐标的Excel编号导出到单独的Excel文件中。
for i = 1:k idx_i = find(idx==i); idx_excel = data(idx_i,1); file_name = strcat('cluster_', num2str(i), '.xlsx'); xlswrite(file_name, idx_excel); end
完整代码如下:
data = xlsread('coordinates.xlsx');
coordinates = data(:,2:3);
k = 8;
[idx, centers] = kmeans(coordinates, k);
colors = ['r','g','b','c','m','y','k','w'];
figure;
hold on;
for i = 1:k
scatter(coordinates(idx==i,1), coordinates(idx==i,2), 5, colors(i), 'filled');
end
scatter(centers(:,1), centers(:,2), 50, 'k', 'filled', 'Marker','o','LineWidth',2);
title('K-means Clustering');
xlabel('Longitude');
ylabel('Latitude');
center_excel = [1:k; centers'];
xlswrite('centers.xlsx', center_excel);
for i = 1:k
idx_i = find(idx==i);
idx_excel = data(idx_i,1);
file_name = strcat('cluster_', num2str(i), '.xlsx');
xlswrite(file_name, idx_excel);
end
相关推荐
















