Sum every n-th row in table

3 次查看(过去 30 天)
Dear MATLAB experts,
I'm trying to sum 3 rows at a time of a table column and create a new varible in another table with the values of the sums of the other table. I have found the following post that deals with this: https://de.mathworks.com/matlabcentral/answers/409290-how-can-i-sum-every-nth-row. However, this code only works with arrays and I'm trying to do apply this approach to a table, which I haven't managed to do.
I'm trying to sum 3 rows at a time from the column nr. 8 of the table 'abnormalReturnsTable90' and displaying each one of these sums in a row at a time of column nr. 3 in 'carTable'.
Thank you in advance

采纳的回答

Sahil Jain
Sahil Jain 2021-10-21
Hi. You can use the same approaches that have been suggested in the answers that you have linked. The only difference would be that to access data in a table, curly braces "{}" would be used instead of parenthesis "()". For example,
A = table(rand(666681,1)); % dummy data
[n,col] = size(A);
index = 1:n;
elem = [repmat(3,1,floor(n/3))];
endv = n-sum(elem);
if(~endv)
endv = [];
end
index = mat2cell(index,1,[elem,endv])';
B = cell2mat(cellfun(@(x) sum(A{x,:},1),index,'un',0)); % braces used for accessing table data
carTable.newColumn = B;
You can learn more about accessing data in tables from the Access Data In Tables documentation page.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by