Direct calculations on tables - elementwise mean of n-tables
1 次查看(过去 30 天)
显示 更早的评论
I am using 2023b so https://www.mathworks.com/help/matlab/matlab_prog/direct-calculations-on-tables-and-timetables.html should apply.
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
2023-11-29
编辑:Dyuman Joshi
2023-11-29
You should take a look at what the output of data{1:end} is.
This should be helpful - https://la.mathworks.com/matlabcentral/answers/1656435-tutorial-comma-separated-lists-and-how-to-use-them
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
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
0 个评论
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
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)
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
M = varfun(@(x)mean(x,2),T)
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".
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!