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!