Aggregation by index in first column

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

3 个评论

I stopped here already, because I did not get it:
day = [1, 2, 1; ...
1, 2, 1; ...
1, 2, 1; ...
1, 2, 1; ...
1, 2, 1];
daysize = size(day);
If this is the input, what does this explanation mean:
as day 1 will occur next month again and so on
?
Hey Jan, thank you for your answer.
Let me try to simplify myself.
If i had the following day index vector a = [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1]' [rows_a,~]=size(a);
and an arbitrary consumption vector, b = [1:rows_a]'
using accumarray would "almost" solve it, however, it will sum up the elements where day 1 occurs again after the third day. It would sort of have to restart.
A = accumarray(a,b)
Hope you understand.
Appreciate your answer, thank you!
I've overseen that days is a vector, not a matrix. See my 2nd answer.

请先登录,再进行评论。

 采纳的回答

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

2 个评论

[uDay, ~, iU] = unique(days, 'rows'); Result = splitapply(@sum, consump, iU)
will unfortunately do the same as
Result = accumarray(days,consump)
it doesn't distinguish the first ones with those after the threes
Best regards
Jan
Jan 2018-1-10
编辑:Jan 2018-1-10
See my 2nd answer...

请先登录,再进行评论。

更多回答(1 个)

Ah, I get it now. So the actual problem is to convert
days = [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1]
to
days = [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4]
Correct? Then accumarray solves the problem directly.
[~, N] = RunLength(days);
udays = RunLength(1:numel(N), N);
A = accumarray(udays, consump);
And without the fast C-mex RunLength:
x = days(:);
d = [true; diff(x) ~= 0]; % TRUE if values change
n = diff(find([d', true])); % Number of repetitions
udays = repelem(1:numel(n), n); % Need Matlab >= R2015a
A = accumarray(udays, consump);

3 个评论

Hey Jan, thank you for your time! Yes, thats the point. If the data could be converted to ONLY continuous day numbers, it would work with accumarray(). I prefer to use the code without C-mex RunLength as the project has to be delivered to someone. However, when I run the code...
x = days(:);
d = [true; diff(x) ~= 0]; % TRUE if values change
n = diff(find([d', true])); % Number of repetitions
udays = repelem(1:numel(n), n); % Need Matlab >= R2015a
A = accumarray(udays, consump);
I get the following error
Error using accumarray Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
Best regards - Frederik
Ahh, its transposed ;)
d = [true; diff(day) ~= 0]; % TRUE if values change
n = diff(find([d', true])); % Number of repetitions
udays = repelem(1:numel(n), n); %
udays = udays';
A = accumarray(udays, consump);
as my consumption vector was a column, the udays simply had to be transposed. Thanks a lot for your help! Now I can remove a lot of loops which previously did this process!
Thanks !

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by