如何在MATLAB中表示集群?

时间:2016-01-01 00:49:21

标签: algorithm matlab k-means

假设我有以下数据集:

A

1   8   9   12
2   1   0   35
7   0   0   23

6 3 
1 9
0 7

我想要做的是B中的每一行,找到最小值并获得它所出现的列索引。例如,对于 B 中的第1行,最小值为 3 来自 2 列。因此,将第1行从 A 添加到Cluster 2

对于B的第2行,最小值 1 ,来自列1。因此,将第2行从 A 添加到Cluster 1。等等...

现在我想创建一个名为 C 的数组(这将代表我的群集),包含2个项目。第1项包含来自A的所有行的矩阵,该矩阵应该在Cluster 1中,而第2项包含来自A的所有行的矩阵,该矩阵应该在Cluster 2中。这是我遇到问题的地方。这是我目前的尝试:

function clusterSet = buildClusters(A, B)
clusterSet = zeros(size(B, 2)); % Number of clusters = number of columns in B
for i = 1:size(A, 1)
    [value, index] = min(B(i,:)); % Get the minimum value of B in row i, and its index (column number)
    clusterSet(index) = A(i,:); % Add row i from A to its corresponding cluster's matrix. 
end
end

我在最后一行收到以下错误(注意:这并没有明确指出我的数据集'A'和'B',但是谈到了一般的A和B):

In an assignment  A(I) = B, the number of elements in B and I must
be the same.

如果第1行中 B 的最小值来自第2列,则应将A中的第1行添加到矩阵Cluster 2中(B行对应于A的哪一行)添加到集群,B列表示要将其添加到哪个集群。这就是我想要那条线,但我得到了上述错误。

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

这是一种没有循环的方式:

[~, cluster] = min(B,[],2); %// get cluster index of each row
[clusterSort, indSort] = sort(cluster); %// sort cluster indices
sz = accumarray(clusterSort,1); %// size of each cluster
C = mat2cell(A(indSort,:), sz); %// split A into cell array based on clusters