Sum every n-th row in table

4 visualizzazioni (ultimi 30 giorni)
chiefjia
chiefjia il 18 Ott 2021
Modificato: chiefjia il 21 Ott 2021
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

Risposta accettata

Sahil Jain
Sahil Jain il 21 Ott 2021
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.

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by