MATLAB: How to sum over grouped data in a table

arraycategoricalgroupgrp2idxgrpstatsMATLABsum

Suppose I have a table like this toy example:
>> groups = { 'a'; 'a'; 'b'; 'b'; 'c'; 'c'};
>> values = [1;2;3;4;5;6];
>> exampletable = table(groups, values);
I want another column in the table that finds the sum of the values for each group. How can I do this?

Best Answer

For MATLAB R2018b and later:
If you only require a summary table containing the sum for each group, use "groupsummary".
>> G = groupsummary(exampletable,'groups','sum')
G =
3×3 table
groups GroupCount sum_values
______ __________ __________
'a' 2 3
'b' 2 7
'c' 2 11
Use "grouptransform" to both perform the sum calculation by group and expand the result back to your original "exampletable".
>> exampletable = grouptransform(exampletable,'groups',@sum,'ReplaceValues',false)
exampletable =
6×3 table
groups values fun_values
______ ______ __________
'a' 1 3
'a' 2 3
'b' 3 7
'b' 4 7
'c' 5 11
'c' 6 11
The documentation pages for "groupsummary" and "grouptransform" are below.
For MATLAB R2018a and earlier:
You can use a for loop to calculate the sums in a new, preallocated vector. Then, append this vector to the end of your original table.
y = zeros(size(groups));
for i = 1:length(groups)
y(i) = sum(values(ismember(groups,groups(i))));
end
exampletable.sum_values = y;