How to get average/max/min table of many tables
25 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Image Analyst
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.
更多回答(1 个)
Peter Perkins
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
2 个评论
Peter Perkins
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!