Matlab中随机数的K均值聚类
K-Means Clustering of random numbers in Matlab
我有一个程序在运行时生成 10 个固定点和 3 个随机点。我希望这 10 个固定点使用 K 均值聚类,但不知道从哪里开始。我的代码如下
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
上面块中的这个特定片段生成了需要聚类的 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
从你的问题中根本不清楚你想用 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
中定义了每个点被分类到的集群。
我有一个程序在运行时生成 10 个固定点和 3 个随机点。我希望这 10 个固定点使用 K 均值聚类,但不知道从哪里开始。我的代码如下
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
上面块中的这个特定片段生成了需要聚类的 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
从你的问题中根本不清楚你想用 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
中定义了每个点被分类到的集群。