Sum every n-th row in table
조회 수: 4 (최근 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
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!