A problem with K-means Clustering

3 次查看(过去 30 天)
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?

采纳的回答

Cris LaPierre
Cris LaPierre 2022-6-14
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 个评论
Emanuele Gandolfi
Emanuele Gandolfi 2022-6-14
Ok but the problem described in the question is solved. Here I am not able to send all the code
Cris LaPierre
Cris LaPierre 2022-6-14
Ok, then you can mark this question as answered. If you have a new question, feel free to start a new post for it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cluster Analysis and Anomaly Detection 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by