Sum every n-th row in table

3 ビュー (過去 30 日間)
chiefjia
chiefjia 2021 年 10 月 18 日
編集済み: chiefjia 2021 年 10 月 21 日
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.
  1 件のコメント
chiefjia
chiefjia 2021 年 10 月 21 日
Thank you Sahil!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by