generate out put names and output number dynamically
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 9 Feb 2023
Edited: Stephen23
on 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
on 9 Feb 2023
@Stephen23 I think OP is trying to use arrays but failed in the attempt as they have mentioned.
Accepted Answer
Stephen23
on 9 Feb 2023
Edited: Stephen23
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!