Direct calculations on tables - elementwise mean of n-tables

4 ビュー (過去 30 日間)
cdlapoin
cdlapoin 2023 年 11 月 29 日
回答済み: Steven Lord 2023 年 11 月 29 日
data is a 1x31 cell array with each cell containing a table. Each table is 100x14 numeric data.
I want to get an elementwise mean of the 31 tables. So a single 100x14 table where value(1,1) is a mean of all the values at the (1,1) position of all 31 tables.
>> mean(data{1:end})
Error using tabular/mean
Argument at position 2 cannot be of type 'table'.
I should add that:
(data{1}+data{2}..+data{31})/31
does work. Is there a simpler way? I would like this to still work if the number of tables changes.
Thanks
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 29 日
編集済み: Dyuman Joshi 2023 年 11 月 29 日
You should take a look at what the output of data{1:end} is.
Also, if you have numeric data, why not just store it in a numeric array?
The question here is - How do you obtain the data in such a complicated and obfuscated manner?

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2023 年 11 月 29 日
cellfun table2array, cat(3) the expansion of the resulting cell, mean() across the third dimension. array2table the results giving the variable names from the original table

Peter Perkins
Peter Perkins 2023 年 11 月 29 日
If I understand your situation correctly, data{1:end} is a "comma-separated list" of tables, and so mean(data{1:end}) in effect calls mean like this:
t1 = array2table(rand(5,3));
t2 = array2table(rand(5,3));
try, mean(t1,t2), catch, lasterr, end
ans =
'Error using tabular/mean Argument at position 2 cannot be of type 'table'.'
I don't know of a function in MATLAB that takes the mean across multiple inputs. You'd normally have one array and take the mean across one or more dimensions of that array. In this case, tables can only be 2-D, so that exact thing is not an option. You can of course write a loop
for i = 1:5, C{i} = array2table(rand(10,3)); end % set up data
M = C{1};
for i = 2:length(C)
M = M + C{i};
end
M = M ./ length(C)
M = 10×3 table
Var1 Var2 Var3 _______ _______ _______ 0.56204 0.38388 0.56167 0.42211 0.66402 0.37256 0.66128 0.37039 0.35738 0.61288 0.36267 0.43646 0.4515 0.54206 0.49305 0.51918 0.81702 0.57308 0.58045 0.57443 0.43987 0.66521 0.65924 0.351 0.68491 0.49471 0.73781 0.44043 0.59032 0.47729
Another possibility is to recognize that while you cannot create a 3-D table, you can get the same effect in a couple ways. Here's one: You have 31 tables, each 100x14. Let's go smaller, make it 5 tables, each 10x3. You can store that as one 10x3 table, whose variables themselves have 5 columns.
T = table(rand(10,5),rand(10,5),rand(10,5)) % set up data
T = 10×3 table
Var1 Var2 Var3 ________________________________________________________ _______________________________________________________ _______________________________________________________ 0.69515 0.34155 0.14358 0.42777 0.8565 0.97925 0.10175 0.50921 0.58514 0.35653 0.58279 0.077983 0.82027 0.15634 0.25756 0.96487 0.26338 0.66849 0.058966 0.78839 0.37415 0.35979 0.29546 0.67698 0.65359 0.92431 0.61466 0.81929 0.1626 0.75446 0.50289 0.97632 0.44443 0.91183 0.16221 0.23667 0.084327 0.02943 0.84575 0.8779 0.55676 0.69436 0.32318 0.82205 0.4438 0.62448 0.16535 0.98912 0.76258 0.1475 0.4528 0.05729 0.19463 0.49157 0.89347 0.62577 0.040503 0.6576 0.2829 0.96178 0.11335 0.27417 0.71447 0.15662 0.27734 0.9946 0.038669 0.79637 0.27663 0.27041 0.11215 0.96589 0.72851 0.91485 0.18475 0.55129 0.41562 0.28235 0.27867 0.93569 0.57767 0.86175 0.28731 0.96534 0.71124 0.76623 0.88383 0.91516 0.14905 0.15081 0.17369 0.38477 0.99411 0.81887 0.31804 0.10754 0.10205 0.92865 0.60687 0.78199 0.24429 0.035144 0.957 0.91845 0.97701 0.42023 0.051407 0.60431 0.42962 0.89987 0.90806 0.038941 0.31085 0.57667 0.2785 0.60482 0.17312 0.63075 0.076272 0.99324 0.81019 0.076705 0.90136 0.3256 0.94997 0.45983 0.061559 0.83227 0.48311 0.81865 0.38909 0.18513 0.87467 0.32864 0.53267 0.025399 0.57591 0.83412 0.24811 0.080109 0.49824 0.15185 0.2415 0.6494 0.81304 0.91153 0.60703 0.57795 0.2299 0.31611
M = varfun(@(x)mean(x,2),T)
M = 10×3 table
Fun_Var1 Fun_Var2 Fun_Var3 ________ ________ ________ 0.49291 0.50638 0.37899 0.54882 0.47199 0.65506 0.59953 0.41482 0.56803 0.53781 0.41795 0.51371 0.30719 0.47534 0.58123 0.49272 0.68066 0.57302 0.5379 0.50542 0.62638 0.48108 0.4226 0.49564 0.61277 0.53108 0.46204 0.35273 0.47081 0.52851
That "embed multiple tables in one" storage scheme may be less convenient for whatever else you need to do, but it's worth considering. It's a reasonable scheme if your 31 tables are something like "replicate data".

Steven Lord
Steven Lord 2023 年 11 月 29 日
Is there a simpler way?
Yes. If you know that your data cell array is not empty, just use a for loop.
n = numel(data);
if n > 0
S = data{1};
for whichone = 2:n
S = S + data{whichone};
end
S = S./n;
else
% What should S be in the case data is empty?
end

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by