How to use create table name variables in loop?

조회 수: 1 (최근 30일)
Summer Bolton
Summer Bolton 2021년 9월 12일
댓글: Star Strider 2021년 9월 12일
%% Divide into groups
K=10;
group_total= round(n/K);
Group_name= {'Group1' 'Group2' 'Group3' 'Group4' 'Group5' 'Group6' 'Group7' 'Group8' 'Group9' 'Group10'};
for x=1:1:K
for g=1:1:group_total
num= x* g;
s.(Group_name{x}(g,:)) = randomdata(num,:);
end
end
I have a table that is 8143 x 10 (randomdata). I need to divide it into 10 smaller tables by row number. I should have 10 (approx) 814x10 tables. I'm trying to use s as struct but I don't know how to make the Group_name{x} a 814 x10 table instead of a 1x10 table.
K is the number I need to divide the main table(random data) by.
group_total is the number of rows divided by K
s is the struct

채택된 답변

Star Strider
Star Strider 2021년 9월 12일
I am not certain that I understand what you want to do.
Perhaps —
randomdata = randn(33,10);
NrCellArrays = fix(size(randomdata,1)/10)-1;
for k = 1:NrCellArrays
idxrng = (1:10)+10*(k-1);
Group{k,:} = randomdata(idxrng,:);
end
Group{k+1,:} = randomdata(max(idxrng):size(randomdata,1),:)
Group = 3×1 cell array
{10×10 double} {10×10 double} {14×10 double}
In any event, just put them into a cell array and refer to them by subsecripts (such as Group{1} and so forth). If at all possible, so not use numbered variables if a collection of objects (such as here) is the desired result.
.
  댓글 수: 5
Summer Bolton
Summer Bolton 2021년 9월 12일
That is exactly what I want, thank you so much!!
It also took me awhile to realize that to access the data inside, say Group 3 column 4, the syntax is Group{3}(3,:); for anyone else who has this question!
Star Strider
Star Strider 2021년 9월 12일
As always, my pleasure!
The example you wrote returns row 3 of Group 3.
To access Group 3, column 4:
Group{3}(:,4)
See Access Data In A Cell Array for an extended description.
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by