generate out put names and output number dynamically
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Im have hard code K but I want to change it so K will be generated i have done that part
the problem is G1 to G5 veriables should be change from G1 to GK
how can I do this
K=5 % specify and hard code K number of clastors
X;
[IDX, centr] = kmeans( X , K , 'distance' , 'sqEuclidean');% K number of clustors
% separate the data into five groups
G1 = Y2(IDX == 1 , : );
G2 = Y2(IDX == 2 , : );
G3 = Y2(IDX == 3 , : );
G4 = Y2(IDX == 4 , : );
G5 = Y2(IDX == 5 , : );
I try this simply didt work
[IDX, centr] = kmeans( X , K , 'distance' , 'sqEuclidean');% K number of clustors
for g=1:K
G(g) = Y2(IDX == g , : )
end
3 Commenti
Stephen23
il 9 Feb 2023
Modificato: Stephen23
il 9 Feb 2023
"the problem is G1 to G5 veriables should be change from G1 to GK"
The problem is that you are forcing meta-data (i.e. pseudo-indices) into variable names.
"how can I do this "
Rather than attempting to force pseudo-indices into variable names, you should be using actual indices.
How to use arrays, loops, and indexing is explained in the introductory tutorials:
The name "MATLAB" comes from "MATrix LABoratory", not from "lets split up the data into lots and lots of separate variables and make working with the data slow and complex". MATLAB is designed to work with arrays. You should use arrays.
Dyuman Joshi
il 9 Feb 2023
@Stephen23 I think OP is trying to use arrays but failed in the attempt as they have mentioned.
Risposta accettata
Stephen23
il 9 Feb 2023
Modificato: Stephen23
il 9 Feb 2023
Because you did not upload any data I will use an inbuilt dataset:
S = load('fisheriris.mat');
X = S.meas(:,3:4);
Now lets group that data:
[idx,C] = kmeans(X,2) % keep it simple
F = @(n) X(idx==n,:);
C = arrayfun(F,1:max(idx),'uni',0)
Checking:
C{:}
Note that splitting up data is often not a good approach, it is the kind of thing that users do because they think it is required for processing of their data. In fact, MATLAB has tools that support working on entire datsets without splitting them up into separate arrays:
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!