A problem with K-means Clustering
3 views (last 30 days)
Show older comments
Emanuele Gandolfi
on 14 Jun 2022
Commented: Cris LaPierre
on 14 Jun 2022
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
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?
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!