# Arrange clustered rows based on their label value

조회 수: 2(최근 30일)
Elysi Cochin 2022년 11월 11일
답변: Askic V 2022년 11월 11일
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
In my example instead of 12 it will be 42.
Please could someone help me to solve it.

댓글을 달려면 로그인하십시오.

### 채택된 답변

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?
% 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

댓글을 달려면 로그인하십시오.

### 범주

Find more on Gaussian Mixture Models in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by