如何在 MATLAB 中表示簇?

How to represent clusters in MATLAB?

假设我有以下数据集:

A:

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

B:

6 3 
1 9
0 7

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

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

现在我想创建一个包含 2 个项目的名为 C 的数组(这将代表我的集群)。项目 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的列代表加入到哪个簇中)。这就是我希望该行执行的操作,但出现上述错误。

有什么建议吗?

这里有一个没有循环的方法:

[~, 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