cumulative sum table over group
Mostrar comentarios más antiguos
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.
1 comentario
Frederick Awuah-Gyasi
el 20 de Mayo de 2022
Editada: Frederick Awuah-Gyasi
el 23 de Mayo de 2022
Respuesta aceptada
Más respuestas (1)
Steven Lord
el 20 de Mayo de 2022
1 voto
Take a look at the grouptransform function.
6 comentarios
Frederick Awuah-Gyasi
el 20 de Mayo de 2022
You can specify a function handle to a function that satisfies certain conditions as the method input.
A = randi(3, 6, 1);
B = randi(10, 6, 1);
T = table(A, B)
grouptransform(T, 'A', @cumsum)
Frederick Awuah-Gyasi
el 20 de Mayo de 2022
"I wish there is a way to take it out do the groupTransform and join to have it back in place."
Try this:
Name = char('A' + randi([0 2], 18, 1));
Day = randi(2, 18, 1);
Color = randi([0 3], 18, 1);
Quantity = randi([0 5], 18, 1);
T = table(Name, Day, Color, Quantity)
T(:,{'Color' 'Quantity'}) = grouptransform(T(:,{'Color' 'Quantity'}), 'Color', @cumsum)
Frederick Awuah-Gyasi
el 20 de Mayo de 2022
Frederick Awuah-Gyasi
el 23 de Mayo de 2022
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
