How to use create table name variables in loop?

%% 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

 采纳的回答

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 个评论

Could you explain this a little more?
Basically I have a table called random data with 8143 rows and 10 columns. I want to divide this into 10 separate tables, so Group1 would contain rows 1 through 814 and include the data in the 10 columns (a 814 x 10 table). Then Group2 would contain rows 815 to 1628 with 10 columns (another 814x 10 table). And I will need to be able to access these separate tables.
Also I ran the code. It looks like it made 814 10x10 tables, instead of 10 814 x 10 tables.
Of course!
I initially coded it as a ‘proxy solution’ simply to demonstrate the approach.
A more appropriate description, using the numbers you want, is —
randomdata = randn(8143,10);
NrRows = size(randomdata,1);
NrCells = fix(NrRows/10);
NrCellArrays = fix(NrRows/NrCells)-1;
for k = 1:NrCellArrays
idxrng = (1:NrCells)+NrCells*(k-1);
Group{k,:} = randomdata(idxrng,:);
end
Group{k+1,:} = randomdata((max(idxrng)+1):size(randomdata,1),:)
Group = 10×1 cell array
{814×10 double} {814×10 double} {814×10 double} {814×10 double} {814×10 double} {814×10 double} {814×10 double} {814×10 double} {814×10 double} {817×10 double}
Then —
Group{1}
ans = 814×10
-0.0762 0.4418 0.9042 -1.0105 0.8444 0.9038 -0.9097 -1.2640 -0.3545 0.6364 0.5437 -0.9377 0.5214 1.3654 2.3390 -0.2568 -0.2186 0.0125 -0.3477 -0.3256 -1.0349 -0.5559 -0.7966 0.7491 -0.1424 2.4231 0.8458 -0.0352 -0.2379 -0.5524 -0.0841 0.1164 1.0977 -0.4539 -0.8268 -0.5018 1.3271 0.1632 1.3520 -0.9521 -0.4435 0.3795 -1.4061 0.6939 -0.2882 -1.6030 -0.4384 -0.0879 1.4618 -0.1191 -2.2813 -0.0392 1.9152 0.4012 0.4568 0.0209 -0.4258 -1.0751 0.4075 -0.2689 -1.1726 -0.0926 0.0498 -0.6852 0.0724 -0.1043 -0.8621 -0.7717 0.1280 0.0925 -1.6290 1.0042 -1.1736 0.2177 1.1849 -0.2645 -0.8989 -0.6456 1.0928 -1.1829 0.9889 -1.2836 0.2037 1.1415 -0.4152 -0.6046 -0.4038 1.2737 0.1878 -1.4318 -0.2531 0.2005 -0.2591 2.9833 1.3243 -2.0361 -1.1488 -0.6434 -0.4460 0.1135
and
Group{10}
ans = 817×10
0.6032 0.6403 0.2983 -0.5929 -0.6918 0.3273 1.4795 0.9936 -0.9651 0.2107 2.0698 0.4010 -0.4238 -0.8070 -1.7096 0.0908 1.2975 -0.0043 -0.4132 0.3445 -0.4531 0.5483 -1.3479 0.0735 -0.2774 1.8706 -0.6773 0.2755 0.3299 -0.9899 -1.2002 -1.0462 0.1717 -0.0484 0.9020 -1.4983 -0.5015 0.0223 -1.7609 -2.5133 -1.3007 0.9171 0.0638 -0.8745 1.2826 0.5071 -0.5058 0.7269 -0.5637 0.7827 2.1734 -0.7690 1.0559 0.4129 0.8538 0.1729 -0.9504 0.3720 -0.9369 0.3036 -0.7351 1.1439 -3.0809 1.0199 -1.7711 -2.0174 -0.6506 -1.8269 1.1292 -1.0364 1.2538 -0.2299 0.0531 0.0771 2.1184 -0.7864 -0.3270 -0.4896 -1.4754 -0.3230 0.4377 -0.3632 0.2201 3.3904 1.2465 -0.7594 0.6201 -0.2400 0.6353 -0.0365 -0.3378 -0.4052 -0.1581 -0.6805 -0.2422 -0.3169 0.4323 0.8359 -2.0499 0.0689
would be the way to refer to each of them.
That should be close to what you want.
.
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!
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 个)

类别

帮助中心File Exchange 中查找有关 Tables 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by