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?

 Réponse acceptée

Cris LaPierre
Cris LaPierre le 14 Juin 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 commentaires

Emanuele Gandolfi
Emanuele Gandolfi le 14 Juin 2022
I am developing a code where I am going to analyze different clustering techniques that allow me to merge users who will then be served by antennas. However this is an alternative code (but it works the same way) because the original file is made up of multiple files.
I had sensed that sumd was not the same size. To solve then I could use the reshape function so that X has only two columns?
The rows of X correspond to observations, and the columns correspond to variables. Said another way, you have collected 74 data points on 1482 people.
Reshaping your data may be an approach to take here, but only if it makes sense to do so. kmeans can certainly cluster data in 74 dimensions, but it can be hard to wrap your head around what a cluster means. I still don't understand enough of what you are trying to do offer any advice.
Here's an example of visualizing the first 2 variables of your data.
X = rand(1482,74);
[idx,C,sumd] = kmeans(X,2);
figure
gscatter(X(:,1),X(:,2),idx,'bg')
hold on
plot(C(:,1),C(:,2),'kx')
hold off
legend('Cluster 1','Cluster 2','Cluster Centroid')
Emanuele Gandolfi
Emanuele Gandolfi le 14 Juin 2022
Adding the output C the error is gone, but now it gives me another error. Is there a way to contact you so that I can send you the complete file, so you also understand my mistake?
Cris LaPierre
Cris LaPierre le 14 Juin 2022
It is best to use this forum, as that way it benefits the entire community.
Emanuele Gandolfi
Emanuele Gandolfi le 14 Juin 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 le 14 Juin 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by