1
我正在MATLAB中使用k-means。这里是我的代码:基于质心拟合群集数
load cobat.txt; % read the file
k=input('Enter a number: '); % determine the number of cluster
isRand=0; % 0 -> sequeantial initialization
% 1 -> random initialization
[maxRow, maxCol]=size(cobat);
if maxRow<=k,
y=[m, 1:maxRow];
else
% initial value of centroid
if isRand,
p = randperm(size(cobat,1)); % random initialization
for i=1:k
c(i,:)=cobat(p(i),:) ;
end
else
for i=1:k
c(i,:)=cobat(i,:); % sequential initialization
end
end
temp=zeros(maxRow,1); % initialize as zero vector
u=0;
while 1,
d=DistMatrix3(cobat,c); % calculate the distance
[z,g]=min(d,[],2); % set the matrix g group
if g==temp, % if the iteration doesn't change anymore
break; % stop the iteration
else
temp=g; % copy the matrix to the temporary variable
end
for i=1:k
f=find(g==i);
if f % calculate the new centroid
c(i,:)=mean(cobat(find(g==i),:),1)
end
end
c
sort(c)
end
y=[cobat,g]
“cobat”是我的文件。它看起来如下:
65 80 55
45 75 78
36 67 66
65 78 88
79 80 72
77 85 65
76 77 79
65 67 88
85 76 88
56 76 65
“c”是每个群集的质心(群集的中心)的变量。 “g”是显示簇号的变量。问题是,我想基于质心(c)对群集数量(从小到大)进行排序/拟合。所以,我尝试对(c)进行排序,但它不影响簇号(g)。
当我尝试排序(g)时,它的排序与我想要的不同。我想要根据质心对簇号进行排序。例;当我运行,其中k = 3的代码,这里是最后的心
73.0000 79.0000 70.6667 %C 1
58.3333 73.3333 84.6667 %C 2
36.0000 67.0000 66.0000 %C 3
当我对它进行排序,数字集群也是“分类”,
36.0000 67.0000 66.0000 %C 3
58.3333 73.3333 70.6667 %C 2
73.0000 79.0000 84.6667 %C 1
我希望它的数量集群适合,像这样。
36.0000 67.0000 66.0000 %C 1
58.3333 73.3333 70.6667 %C 2
73.0000 79.0000 84.6667 %C 3
它很合适,没有排序,所以当这条线'y = [cobat,g]'运行时,它也会改变。
这似乎很容易,但棘手。我无法弄清楚。任何人有任何想法来解决它?
谢谢。
谢谢你的答案。 我在哪里放置该脚本? 我把它放在这行后面:'sort(c)',这是错误的。我尝试了另一个地方(在'y = [cobat,g]'行之前),它出现同样的错误: '???索引超过矩阵尺寸。 错误==> clustere在49 C = C(索引);' 你的答案将是这样的帮助和理解了很多。 :) – 2013-05-05 09:22:56
这些行应该取代您目前正在进行的排序。如果使用'sort'而不是'sortrows',那么使用'[g index] = sort(g); c = c(索引);' – Shai 2013-05-05 09:27:24
再次感谢你的答案。 :)我试过了,错误仍然是一样的:'???索引超过矩阵尺寸。 错误在==> clustere在47 c = c(index);'它说“超出矩阵维度”。我的文件有3维,那么如何让代码适合3维? :) – 2013-05-05 09:52:01