Arrange clustered rows based on their label value
1 次查看(过去 30 天)
显示 更早的评论
Suppose I have a matrix of size 100 x 42.
Now I do kmeans clustering
[idx,C] = kmeans(XTrain,8);
Suppose the first 10 rows of XTrain have idx 1
I need to get the labels from YTrain
Suppose the labels of YTrain are
[1 1 1 2 2 2 2 3 3 3];
Now I want to store the 10 rows of XTrain having idx = 1, according to the label YTrain in a cell array to get a cell array of size
42 x 3
42 x 4
42 x 3
I need to do the same with the attached mat-file, so that I finally get a cell array newXTrain and new label newYTrain as shown in the attached image Output.png
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1188873/image.png)
In my example instead of 12 it will be 42.
Please could someone help me to solve it.
0 个评论
采纳的回答
Askic V
2022-11-11
Hello Elysi,
maybe I'm missing something here, but according to your code, you partition input data into 8 clusters, but your YTrain array containes 22 unique values. So would it be natural to cluster the input data into 22 clusters based on the distance (Euclidean distance)?
Do you need something like this?
load XY.mat
% Number of clusters
nrClusters = 8;
% partition the observations of the data matrix XTrain into nrClusters
[idx, C] = kmeans(XTrain, nrClusters);
% Cell array will have nrClusters x 2
% Each row of the cell array will contain rows from XTrain
% Grouped together based on the idx cluster they belong to.
cArray = cell(nrClusters, 2);
% For each row in XTrain determin to which cArray element it belongs to
for i = 1:size(XTrain, 1)
% Add row from XTrain to existing rows with the same cluster index
cArray{idx(i),1} = [cArray{idx(i),1}; XTrain(i,:)];
cArray{idx(i),2} = idx(i);
end
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!