How to use create table name variables in loop?

6 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by