for i=1:max_g sprintf('迭代次数%d',i) %分类 L1=zeros(1,size(P,1));%建立一个1行12列的数组 for j=1:size(P,1) d=zeros(1,k);%不同类簇的个数 for m=1:k d(m)=norm([P(j,:)-temp1a(m,:)]);%计算欧氏距离,norm表示范数,P(j,:)和temp1a(m,:)是两点坐标,差即为两点之间的距离向量,然后计算该向量的范数,即可得到欧几里得距离。 end [~,index]=min(d);%把欧式距离更新为当前的最小值 L1(j)=index; end LL1(i,:)=L1;%将L1向量赋值给LL1矩阵的第i行 temp2a=temp1a; for j=1:k T=find(L1==j);%j=3 L1中所有等于j的元素的下标放入T中储存 if size(T,2)~=0 temp2a(j,:)=sum(P(T,:))/size(T,2);%将P第T列的平均值,存储到temp2a矩阵的第j行中。 else temp2a(j,:)=[min_x+(max_x-min_x)*rand,min_y+(max_y-min_y)*rand];%生成二维随机坐标点。rand函数生成一个0到1之间的随机数,将其乘以对应轴的范围再加上最小值。 end end if i==1 temp1a=temp2a; %temp2a 赋值给 temp1a else if sum(sum(temp2a-temp1a))==0 %当 i>1时,判断temp2a和temp1a的差别是否为零,如果是,则跳出循环。第一个sum生成3*1矩阵,第二个sum生成一个数 break else temp1a=temp2a; %将temp2a赋值给temp1a end end end
时间: 2024-04-25 18:28:01 浏览: 9
这段代码是 K-means 聚类算法的实现,其中 max_g 是最大迭代次数,P 是数据集,k 是要聚成几类,temp1a 是初始的聚类中心点。在算法中,首先将数据集中每个点归到距离最近的聚类中心所在的类别中,然后根据每个类别中的数据点重新计算聚类中心点,直到聚类中心点不再变化或者达到最大迭代次数为止。最后,LL1 矩阵中的每一行表示每一次迭代后数据集中每个点所属的类别。
相关问题
% 输出每个人的任务分配 for i = 1:person_num fprintf('第%d个人的任务分配:\n', i); disp(find(assignment_matrix(:, i))); end
如果您想将输出的任务编号写入表格中,您可以将其输出到一个CSV文件中。可以使用MATLAB的csvwrite命令将数据写入到CSV文件中。以下是一个示例代码,可以将每个人的任务分配写入到名为“assignment.csv”的CSV文件中。
```
% 定义CSV文件名
filename = 'assignment.csv';
% 打开CSV文件进行写入
fid = fopen(filename, 'w');
% 循环遍历每个人的任务分配
for i = 1:person_num
% 获取当前人的任务编号
task_ids = find(assignment_matrix(:, i));
% 将任务编号转换为字符串
task_ids_str = sprintf('%d,', task_ids);
task_ids_str = task_ids_str(1:end-1); % 去除最后一个逗号
% 将任务编号写入到CSV文件中
fprintf(fid, 'Person %d,%s\n', i, task_ids_str);
end
% 关闭CSV文件
fclose(fid);
% 输出写入完成的消息
fprintf('任务分配已写入到文件 %s 中。\n', filename);
```
请注意,上述代码中的“assignment_matrix”是您自己定义的一个变量,用于存储任务分配结果的矩阵。在使用上述代码之前,请确保您已经定义了此变量,并已经将任务分配结果存储在其中。
% 指定包含SEM图像的目录 image_dir = 'D:\MATLAB\R2018a\bin\灰岩12个\样7\500X\'; % 从目录中读取图像文件名列表 image_files = dir(fullfile(image_dir, '*.tiff')); % K-均值聚类的参数 num_clusters = 3; % 簇数(可以更改此值) max_iterations = 100; % 最大迭代次数(可以更改此值) % 初始化矩阵以存储群集映像和群集中心 num_images = numel(image_files); % 计算图像文件数 clustered_images = cell(1, num_images); cluster_centers_all = cell(1, num_images); % 循环浏览每个图像文件 for i = 1:num_images % 读取当前图像并规范化 image_path = fullfile(image_dir, image_files(i).name); image_data = double(imread(image_path))/ 255; % 执行K-means聚类 [cluster_indices, cluster_centers] = kmeans(reshape(image_data,[],size(image_data,3)), num_clusters,'MaxIter',max_iterations); % 将聚集的数据重新整形为图像维度 clustered_images{i} = reshape(cluster_indices, size(image_data,1),size(image_data,2)); % 将聚类图像转换成彩色图像 RGB = zeros(size(image_data)); for j = 1:num_clusters RGB(:,:,j) = (clustered_images{i} == j); end RGB = bsxfun(@times, RGB, reshape(cluster_centers, 1,1,[])); clustered_images{i} = RGB; % 保存聚类后的图像到文件夹 [pathstr, name, ext] = fileparts(image_path); imwrite(uint8(RGB*255), fullfile(pathstr, [name '_clustered' ext])); end % 显示原始图像和群集图像 for i = 1:num_images figure; subplot(1, num_clusters + 1, 1); imshow(imread(fullfile(image_dir, image_files(i).name))); title('Original Image'); for j = 1:num_clusters subplot(1, num_clusters + 1, j + 1); imshow(clustered_images{i}); title(sprintf('Cluster %d', j)); end end % 计算孔隙率 porosity = zeros(1, num_images); for i = 1:num_images % 统计原始图像中的像素数 img_pixels = numel(imread(fullfile(image_dir, image_files(i).name))); % 统计聚类图像中标记为第一个簇的像素数 cluster_pixels = sum(sum(clustered_images{i}(:,:,1) > 0)); % 计算孔隙率 porosity(i)=(1 - (cluster_pixels / img_pixels))*100; end % 显示计算后的孔隙率 for i = 1:num_images fprintf('Image %d: Porosity = %f\n', i, porosity(i)); end
这是一段 MATLAB 代码,对一组 SEM 图像执行了 K-means 聚类,并计算了孔隙率。其中,通过指定包含 SEM 图像的目录,使用 `dir` 函数读取图像文件名列表,然后使用 `imread` 函数读取图像数据并规范化。接着,使用 `kmeans` 函数对图像进行 K-means 聚类,将聚类图像转换成彩色图像,并保存聚类后的图像到文件夹。最后,计算每个图像的孔隙率,并输出计算结果。