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 comentarios

Jan
Jan el 10 de En. de 2018
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
?
Frederik Skøtt
Frederik Skøtt el 10 de En. de 2018
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!
Jan
Jan el 10 de En. de 2018
I've overseen that days is a vector, not a matrix. See my 2nd answer.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 10 de En. de 2018
Maybe you want:
[uDay, ~, iU] = unqiue(days, 'rows');
Result = splitapply(@sum, consump, iU);

2 comentarios

Frederik Skøtt
Frederik Skøtt el 10 de En. de 2018
[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 el 10 de En. de 2018
Editada: Jan el 10 de En. de 2018
See my 2nd answer...

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 10 de En. de 2018
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 comentarios

Frederik Skøtt
Frederik Skøtt el 11 de En. de 2018
Editada: Frederik Skøtt el 11 de En. de 2018
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
Frederik Skøtt
Frederik Skøtt el 11 de En. de 2018
Ahh, its transposed ;)
Frederik Skøtt
Frederik Skøtt el 11 de En. de 2018
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 !

Iniciar sesión para comentar.

Categorías

Preguntada:

el 10 de En. de 2018

Comentada:

el 11 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by