eyes enberg
eyes enberg

Reputation: 586

How to represent clusters in MATLAB?

Suppose I have the following data sets:

A:

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

B:

6 3 
1 9
0 7

What I want to do is for each row in B, find the smallest value and get the column index from which it appears in. For example, for row 1 from B, the smallest value is 3 which comes from column 2. Therefore add row 1 from A to Cluster 2.

For row 2 from B, the smallest value is 1, which comes from column 1. Therefore add row 2 from A to Cluster 1. And so on...

Now I want to make an array called C (this will represent my clusters) with 2 items. Item 1 contains the matrix of all rows from A that should be in Cluster 1, and Item 2 contains the matrix of all rows from A that should be in Cluster 2. This is where I'm having problems. This is my current attempt:

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

I'm getting the following error on the last line (note: this is not explicitly referring to my data sets 'A' and 'B', but talks about a general A and B):

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

If the minimum value of B in row 1 comes from column 2, then row 1 from A should be added to a matrix Cluster 2 (row of B corresponds to which row of A to add to the cluster, and the column of B represents which cluster to add it to). This is what I want that line to do but I get the above error.

Any suggestions?

Upvotes: 2

Views: 122

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

Here's a way without loops:

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

Upvotes: 3

Related Questions