split table in mat lab with respect to row values

29 次查看(过去 30 天)
Hi i try to write script in matlab so as i can split my table in many others tables. my script return an error mentioned in the following picture which also clarify all my workspace and the description of my table(climSumry):
any one can help me to find where is the error in my script and how i should correct it ?? thank you. Regards

回答(1 个)

Peter Perkins
Peter Perkins 2018-2-17
编辑:Rena Berman 2024-7-16
It's hard to know what you are trying to do, but I'm guessing you want to split up a table, by rows, based on one of the variables in the table. It looks like maybe you have two groups of rows.
I really recommend you take a look at the documentation for tables, including the section on subscripting, but to answer your question,
>> t = table(["a";"a";"a";"b";"b"],rand(5,1),rand(5,1))
t =
5×3 table
Var1 Var2 Var3
____ _______ _______
"a" 0.26187 0.10676
"a" 0.33536 0.65376
"a" 0.67973 0.49417
"b" 0.13655 0.77905
"b" 0.72123 0.71504
>> ta = t(t.Var1=="a",2:3)
ta =
3×2 table
Var2 Var3
_______ _______
0.26187 0.10676
0.33536 0.65376
0.67973 0.49417
>> tb = t(t.Var1=="b",2:3)
tb =
2×2 table
Var2 Var3
_______ _______
0.13655 0.77905
0.72123 0.71504
  2 个评论
Imola Fodor
Imola Fodor 2021-8-4
is there an programatic way to do this? i have a lot of groups, and they are actually compound on 3 different columns
Peter Perkins
Peter Perkins 2021-8-6
Here's one way: use findgroups to identify the groups in your table. Loop from 1:numGroups, and use something like
tables{i} = t(grp == i,2:3)
to put each subtable in a cell array.
BUT: I'm gonna suggest that you probably don't want to do that. Most of the things that you would do on those individual tables in the cell array can be done using groupsummary, groupfilter, grouptransform, rowfun, or maybe others.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by