【问题标题】:K-Means Clustering of random numbers in MatlabMatlab中随机数的K-Means聚类
【发布时间】:2015-03-07 15:36:35
【问题描述】:

我有一个程序在运行时会生成 10 个固定点和 3 个随机点。我希望 10 个固定点使用 K-means 聚类,但不知道从哪里开始。我的代码在下面

function TESTING (re_point)

%***********************NOTE*************************************
% if re_point = 0 [default]
%     points generated for xtemp and y_temp remain fixed
% if re_point = 1
%     new points are generated for x_temp and y_temp

% Variable definitions for tags and figure window
persistent xtemp ytemp hFig

% Initialisiation of re_point
if nargin<1
    re_point = 0; % If 0, the points are fixed, if 1 they move
end

A1 = 30; % area defined as 30 X 30 grid
N = 10;
R = 3; % 3 readers
s = rng; % fixed tags does not change position when simulated repatedly
rng(s)

if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
    % Generate x and y position of tags 
    xtemp = A1*rand(1,N);
    ytemp = A1*rand(1,N);
end
if isempty(hFig)
    hFig = figure;
end

% Generate x and y position of red points 
xtemp_2 = A1*rand(1,R);
ytemp_2 = A1*rand(1,R);

% plot data
plot(xtemp,ytemp,'.',xtemp_2,ytemp_2,'rs','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',14);

% Labelling of the red markers
for iter = 1:numel(xtemp_2)
    text(xtemp_2(iter),ytemp_2(iter), num2str(iter),...
        'FontSize',8,'HorizontalAlignment','center',...
        'Color','White','FontWeight','bold');
end

grid on
hold off
axis([0 A1 0 A1])

% Tag formatting
xoffset = 0;
yoffset = -1;
fsize = 8;
temp_str = mat2cell(num2str([xtemp(:) ytemp(:)], '(%.2f,%.2f)'), ones(1,N));
text(xtemp+xoffset, ytemp+yoffset, temp_str,'fontsize', fsize)

% distance function calculator
cDistance = distanceCalc()

    function S = distanceCalc
        S = size(numel(xtemp),numel(xtemp_2));
        for ri = 1:numel(xtemp)
            for fi = 1:numel(xtemp_2)
                S(ri,fi) = pdist([xtemp(ri),ytemp(ri);...
                            xtemp_2(fi),ytemp_2(fi)],...
                            'euclidean');
            end
        end
    end

end

上面块中的这个特殊的 sn-p 生成需要聚类的 10 个固定点

if (isempty(xtemp) && isempty(xtemp)) || re_point == 1
        % Generate x and y position of tags 
        xtemp = A1*rand(1,N);
        ytemp = A1*rand(1,N);
    end

【问题讨论】:

  • 你的问题不清楚......这是关于如何在matlab中使用kmeans吗?如果是这样,请参阅se.mathworks.com/help/stats/kmeans.html
  • 是的,它在matlab中,我在标题中提到了它。我看过 mathworks 文档,但不明白如何实现它,所以我来这里寻求帮助。
  • 我在下面发布了一个答案,但对这个问题最简单的答案是查看文档中非常清晰的示例。

标签: matlab random plot k-means


【解决方案1】:

您的问题根本不清楚您想用kmeans 做什么,例如您要查找多少个集群?我建议查看MATLAB ref guide中的第一个示例

对于您的数据,您可以尝试例如

X = [xtemp(:) ytemp(:)];
Nclusters = 3;
[idx,C] = kmeans(X,Nclusters);

对于绘制类似以下的内容应该可以:

figure, hold on
plot(X(idx==1,1),X(idx==1,2),'b*')
plot(X(idx==2,1),X(idx==2,2),'g*')
plot(X(idx==3,1),X(idx==3,2),'r*')

开始。这将尝试将您的随机点分类为 3 个集群。每个点被分类到的集群在idx 中定义。

【讨论】:

  • 我想将数据放入您帮助我处理的 Nclusters 中,然后在可能的情况下对各个集群进行颜色编码,同时突出显示质心。您有什么建议还是我应该参考文档?感谢您的帮助,您让我走上了正确的道路。
  • @Duanne 质心在 C 中...对于颜色编码,有很多示例(您自己的代码表明您应该知道如何做到这一点?)但我添加了一种方法.. .
  • 刚写完代码。我以另一种方式进行颜色编码,这比我想象的要容易。谢谢
猜你喜欢
  • 2013-02-14
  • 2015-03-06
  • 2016-05-23
  • 1970-01-01
  • 2015-04-11
  • 2021-08-19
  • 2011-08-13
  • 2013-08-08
  • 2018-01-14
相关资源
最近更新 更多