## How to get average/max/min table of many tables

### QuanCCC (view profile)

on 22 Dec 2018
Latest activity Commented on by Peter Perkins

on 25 Jan 2019

### Image Analyst (view profile)

Hi,
I have a .mat file including many tables with the same size/ thing: var names are the same, row names are the same, but the values in each cell are different (because they are for different scenarios). So is there a way to get a new table with the average cell values for all the tables? And MaxTable, MinTable which has the max/min values in each cell?
Thank you.

### Image Analyst (view profile)

2018 年 12 月 24 日
採用された回答

Have you tried the mean() and max() functions to see if they work with a table. I haven't though they probably won't since tables can contain non-numeric data.
The other option is to just extract each column into an array, and then you can use whatever function you want. Like for 3 tables:
col11 = table1{:, 1};
col21 = table2{:, 1};
col31 = table3{:, 1};
meanCol = mean([col11, col21, col31]);
maxCol = max([col11, col21, col31]);
If you have a lot of them, you can put them into a loop where you read each table from a file, and extract each column one at a time into a 2-D array and then do the math. See The FAQ
Attach your data in .mat files with the paper clip icon if you need more help.

QuanCCC

### QuanCCC (view profile)

2019 年 1 月 9 日
I used table2array process all my tables. Then reshape each table to one column, then connected them into a multi-column array, get max/min/mean for each row.
The answer may be acceptable too. Inclusion, reshape your data until it's a question for rows.

### Peter Perkins (view profile)

2019 年 1 月 23 日

Most likely, the simplest solution is to vertcat all of your tables, adding an ID variable (a "grouping variable") to indicate which original table each row came from. Then use varfun, splitapply, groupsummary (depending on what release tou have) to compute stats on the variables of interest for each "group" of data. For example
>> t1 = array2table(rand(5,2))
t1 =
5×2 table
Var1 Var2
_______ _______
0.81472 0.09754
0.90579 0.2785
0.12699 0.54688
0.91338 0.95751
0.63236 0.96489
>> t2 = array2table(rand(3,2))
t2 =
3×2 table
Var1 Var2
_______ _______
0.15761 0.48538
0.97059 0.80028
0.95717 0.14189
>> t = [t1; t2];
>> t.ID = [1;1;1;1;1;2;2;2]
t =
8×3 table
Var1 Var2 ID
_______ _______ __
0.81472 0.09754 1
0.90579 0.2785 1
0.12699 0.54688 1
0.91338 0.95751 1
0.63236 0.96489 1
0.15761 0.48538 2
0.97059 0.80028 2
0.95717 0.14189 2
>> varfun(@mean,t,'GroupingVariable','ID')
ans =
2×4 table
ID GroupCount mean_Var1 mean_Var2
__ __________ _________ _________
1 5 0.67865 0.56906
2 3 0.69512 0.47585

QuanCCC

### QuanCCC (view profile)

2019 年 1 月 25 日
Good idea. But is there a better way to sign the ID? In most cases, tables are just so large.
Peter Perkins

### Peter Perkins (view profile)

2019 年 1 月 25 日
If you have more than a handfull of variables, the extra memory will be a small percentage. If you use a categorical variable, it will adjust its internal representation to uint8, uint16, etc. depending on how many "groups" you have, i.e. how many mat files.