cumulative sum table over group
조회 수: 8 (최근 30일)
이전 댓글 표시
tableA
Name Day Color Quantity
A 1 0 3
A 1 2 3
A 1 3 3
A 2 0 2
A 2 2 4
A 2 3 5
B 1 0 3
B 1 1 3
B 1 3 3
B 2 0 4
B 2 2 1
B 2 3 0
C 1 0 2
C 1 1 1
C 1 2 3
C 2 0 1
C 2 1 2
C 2 2 3
Results : Cumulative sum for each color over the days.
tableA_cumsum
Name Day Color Quantity
A 1 0 3
A 1 2 3
A 1 3 3
A 2 0 5
A 2 2 7
A 2 3 8
B 1 0 3
B 1 1 3
B 1 3 3
B 2 0 12
B 2 2 1
B 2 3 3
C 1 0 2
C 1 1 1
C 1 2 3
C 2 0 4
C 2 1 3
C 2 2 6
Was trying cumsum but not sure how to get the groupings done.
채택된 답변
Bruno Luong
2022년 5월 20일
편집: Bruno Luong
2022년 5월 20일
tableA = {"A" 1 0 3 ;
"A" 1 2 3 ;
"A" 1 3 3 ;
"A" 2 0 2;
"A" 2 2 4 ;
"A" 2 3 5;
"B" 1 0 3 ;
"B" 1 1 3 ;
"B" 1 3 3 ;
"B" 2 0 4 ;
"B" 2 2 1;
"C" 1 0 2 ;
"C" 1 1 1 ;
"C" 1 2 3 ;
"C" 2 0 1;
"C" 2 1 2;
"C" 2 2 3};
tableA = cell2table(tableA, "VariableNames",["Name" "Day" "Color" "Quantity"])
[~,~,G]=unique([tableA.Name tableA.Color],"rows");
b = zeros(1,max(G));
tableA_cumsum = tableA;
for k=1:length(G)
Gk = G(k);
s = b(Gk) + tableA.Quantity(k);
tableA_cumsum.Quantity(k) = s;
b(Gk) = s;
end
tableA_cumsum
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Hypothesis Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!