How can I expand a table row variable array into multiple table rows
26 ビュー (過去 30 日間)
古いコメントを表示
I have a table, something like this trivial example:
s(1).id="a"; s(1).datatype="this";s(1).val=zeros(34,1);
s(2).id="b"; s(2).datatype="that";s(2).val=zeros(12,1);
s(3).id="c"; s(3).datatype="other";s(3).val=zeros(42,1);
T=struct2table(s)
3×3 table
id val datatype
___ _____________ ________
"a" [34×1 double] "this"
"b" [12×1 double] "that"
"c" [42×1 double] "other"
I would like to expand the table such that the first row would expand to 34 rows, each with a successive value from the val table variable for the original row. I would end up with a table with 34+12+42 rows.
I've tried doing this with a split apply on the table, grouping into the datatype variable, but ultimately I'm iterating through the table and it's slow, on the order of 20 minutes for a table of 70K rows. It seemed that rowfun() would also be slow because it's also iterating through the table. I was wondering if there is some faster way to perform this operation.
Ultimately what I'm trying to do is treat the val variable in my trivial example as a date time value and then generate a timetable using the date times as the row times, in case that makes any difference.
0 件のコメント
回答 (1 件)
Peter Perkins
2018 年 2 月 17 日
Time to go old school:
>> s(1).id = "a"; s(1).datatype = "this"; s(1).val = (1:34)';
>> s(2).id = "b"; s(2).datatype = "that"; s(2).val = (34+(1:12))';
>> s(3).id = "c"; s(3).datatype = "other"; s(3).val = (34+12+(1:42))';
>> T = struct2table(s)
T =
3×3 table
id datatype val
___ ________ _____________
"a" "this" [34×1 double]
"b" "that" [12×1 double]
"c" "other" [42×1 double]
>> T2 = T(repelem(1:height(T),nvals),1:2);
>> T2.val = vertcat(T.val{:})
T2 =
88×3 table
id datatype val
___ ________ ___
"a" "this" 1
"a" "this" 2
"a" "this" 3
"a" "this" 4
"a" "this" 5
"a" "this" 6
"a" "this" 7
"a" "this" 8
[snip]
"c" "other" 81
"c" "other" 82
"c" "other" 83
"c" "other" 84
"c" "other" 85
"c" "other" 86
"c" "other" 87
"c" "other" 88
4 件のコメント
Elizabeth Ramsey
2021 年 7 月 30 日
Any tips on doing something similar to a table with multple "val" type columns? All tables within a row are the same size. I feel like there should be something simple but I'm at a loss
Peter Perkins
2021 年 8 月 2 日
Varfun is the usual way to apply some operation across variables in a table. But of course there's no guarantee that the different vars will expand out the same.
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!