MATLAB实现聚类

时间:2024-08-19 10:06:38
%% Cluster

x = data;    % 传入数据
[h, w] = size(x);
num_cluster = 12;    % 聚类数

T = clusterdata(x, num_cluster);    % T变量中显示每个数据被分到了哪一类

res = zeros(30);
for i = 1 : num_cluster
    % 整合所有数据的类别划分
    % hang为类, 列无特殊意义, 矩阵元素为样本点序号
    res(i, 1:length(find(T(:, 1) == i)')) = find(T(:, 1) == i)';
end

if w_m == 1
    figure
    plot(x(:, 1), 'y*')
else
    if w_m == 2
        figure
        plot(x(:, 1), x(:, 2), 'r*');
    else
        if w_m == 3
            figure
            plot3(x(:, 1), x(:, 2), x(:, 3), 'r*')
        end
    end
end