Azzera filtri
Azzera filtri

Arrange clustered rows based on their label value

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Askic V
Askic V il 11 Nov 2022
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);

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by