MATLAB Answers

Groupsummary on timetable with cell/matrix data

26 views (last 30 days)
Gregory Bolard
Gregory Bolard on 23 Feb 2021
Commented: Gregory Bolard on 23 Feb 2021
Hello,
Is it possible with groupsummary to apply aggregation function to cell or matrix data?
For example, if I want to average the "Matrix" every 1s with the following timetable example:
t Angle Matrix
________ _____ ______________
0.2 sec 180.4 {53×53 double}
0.4 sec 180.4 {53×53 double}
0.6 sec 180.4 {53×53 double}
0.8 sec 180.4 {53×53 double}
1 sec 180.4 {53×53 double}
1.2 sec 180.4 {53×53 double}
1.4 sec 180.4 {53×53 double}
1.6 sec 180.4 {53×53 double}
1.8 sec 180.4 {53×53 double}
Thank you
Gregory
  4 Comments
dpb
dpb on 23 Feb 2021
It's the accumulation mode over minutes I think OP wants, IA. See below for what I think is intended.
I had to go dig in the doc meself; I didn't recall that facility being built into it.

Sign in to comment.

Accepted Answer

dpb
dpb on 23 Feb 2021
Edited: dpb on 23 Feb 2021
Took a little head-scratching, but the following appears to work as desired--
fnMN3=@(c){mean(cat(3,c{:}),3)}; % function to catenate 2D cell array to 3D array, take mean
means=groupsummary(T,'t','minute',fnMN3,'Matrix');
with a sample table here
>> groupsummary(T,'t','minute',fnMN3,'M');
ans =
1×3 table
minute_t GroupCount fun1_M
_______________ __________ ____________
[0 sec, 60 sec] 6 {5×5 double}
>>
that matched for a set of random integers had created to make a dummy table.
t=seconds(0:0.2:1);
M=randi(100,5,5,numel(t));
T=[array2table(t.','VariableNames',{'t'}) vertcat(cell2table(M(:),'VariableNames',{'M'}))]
T =
6×2 table
t M
_______ ____________
0 sec {5×5 double}
0.2 sec {5×5 double}
0.4 sec {5×5 double}
0.6 sec {5×5 double}
0.8 sec {5×5 double}
1 sec {5×5 double}
>> T.M{:}
ans =
31 73 81 86 96
89 71 88 18 42
16 47 46 65 29
62 60 23 68 50
89 19 29 37 81
ans =
29 20 61 50 4
1 49 81 27 55
5 3 30 76 23
11 63 18 30 38
85 11 45 8 30
ans =
74 46 51 97 7
15 25 16 31 31
8 2 59 46 33
94 38 22 91 21
92 65 15 45 18
ans =
73 62 27 84 28
19 22 10 58 22
63 73 94 65 32
29 37 61 50 31
78 23 31 19 89
ans =
65 77 76 72 2
32 33 26 12 22
83 29 100 39 73
29 67 78 92 68
47 3 71 30 52
ans =
19 24 5 97 39
47 32 45 94 24
27 42 77 17 87
49 23 35 55 26
46 54 24 91 83
>> mean(cat(3,T.M{:}),3)
ans =
48.5000 50.3333 50.1667 81.0000 29.3333
33.8333 38.6667 44.3333 40.0000 32.6667
33.6667 32.6667 67.6667 51.3333 46.1667
45.6667 48.0000 39.5000 64.3333 39.0000
72.8333 29.1667 35.8333 38.3333 58.8333
>>
  1 Comment
Gregory Bolard
Gregory Bolard on 23 Feb 2021
Thank you very much, this solves my problem!
Best regards,
Gregory

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!