MATLAB: Aggregation by index in first column

accumulatedaggregatefunctionsindexMATLABsum

Hi!
I have to aggregate some electricity consumption which is provided over a whole year in seconds. Nevertheless, a simple example may illustrate the purpose.
lets say i have day indexes as : a = ones(1,5); b = [2 2 2 2 2]; day = [a b a]'; daysize = size(day);
as day 1 will occur next month again and so on
and a consumption as :
consump = [1:daysize(1)]';
I want to sum up my consumption wherever the same index occurs as my day. Thus, my result should look like the following
result = [sum(consump(1:5)),sum(consump(5:10)),sum(consump(10:15))]'
Eventually, the final result should be :
aggregated_data = [[1 2 1]',result]
Appreciate your time! Cheers Frederik

Best Answer

Maybe you want:
[uDay, ~, iU] = unqiue(days, 'rows');
Result = splitapply(@sum, consump, iU);