How to use create table name variables in loop?

10 visualizaciones (últimos 30 días)
Summer Bolton
Summer Bolton el 12 de Sept. de 2021
Comentada: Star Strider el 12 de Sept. de 2021
%% 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

Respuesta aceptada

Star Strider
Star Strider el 12 de Sept. de 2021
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 comentarios
Summer Bolton
Summer Bolton el 12 de Sept. de 2021
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 el 12 de Sept. de 2021
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.
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by