generate out put names and output number dynamically
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 2월 9일
편집: Stephen23
2023년 2월 9일
"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
2023년 2월 9일
@Stephen23 I think OP is trying to use arrays but failed in the attempt as they have mentioned.
채택된 답변
Stephen23
2023년 2월 9일
편집: Stephen23
2023년 2월 9일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!