A problem with K-means Clustering

3 views (last 30 days)
X = rand(1482,74);
nCluster = 12;
[idx,sumd] = kmeans(X,2);
%%
for pp = 3 : nCluster
[~, index_max_cluster] = max(sumd);
max_wcss_cluster = X(idx==index_max_cluster, :);
[idx,sumd] = kmeans(max_wcss_cluster,2);
end
The logical indices in position 1 contain a true value outside of the array bounds.

'X' appears to be both a function and a variable. If this is unintentional, use 'clear X' to remove the variable 'X' from the workspace.
I need to bisecting K-means to a dataset, but I can't resolve this error. Maybe it's because I have to merge all the data into a cluster first?
How can I solve?

Accepted Answer

Cris LaPierre
Cris LaPierre on 14 Jun 2022
Perhaps you could describe what you think your code is doing?
One thing to be aware of - outputs are determined by position, not variable name. If you want to capture sumd as an output of kmeans, it is captured in the third output.
That would look like this
X = rand(1482,74);
[idx,C,sumd] = kmeans(X,2);
The error you are getting is because you are using idx==index_max_cluster, but since the dimensions don't match (idx is 758x1 while index_max_cluster is 1x74), implicit expansion is turing the comparison into a 758x74 matrix. When used to index X, the matrix is turned into a vector of 758*74=56092 elements. However, X only has 1482 elements. It is the combination of these size mismatches that is generating the error.
  6 Comments
Emanuele Gandolfi
Emanuele Gandolfi on 14 Jun 2022
Ok but the problem described in the question is solved. Here I am not able to send all the code
Cris LaPierre
Cris LaPierre on 14 Jun 2022
Ok, then you can mark this question as answered. If you have a new question, feel free to start a new post for it.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!