Sum every n-th row in table

4 vues (au cours des 30 derniers jours)
chiefjia
chiefjia le 18 Oct 2021
Modifié(e) : chiefjia le 21 Oct 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

Réponse acceptée

Sahil Jain
Sahil Jain le 21 Oct 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.
  1 commentaire
chiefjia
chiefjia le 21 Oct 2021
Thank you Sahil!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by