split table in mat lab with respect to row values
조회 수: 30 (최근 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):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170503/image.png)
any one can help me to find where is the error in my script and how i should correct it ?? thank you. Regards
댓글 수: 0
답변 (1개)
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
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
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 Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!