How to get cumulative sum in yearly buckets
Mostrar comentarios más antiguos
I have several years of data and temperature in one table. I want to calculate a cumulative temperature sum which I've done simply as A.CumulativeTemp= cumsum(A.Temp);
My question is, how can get a cumulative temp for each year, i.e. so it gives a cumulative temperature for the entire 2016, then starts again at zero in the same column and does the same for 2017 and so on?
Kind regards, Wendy
2 comentarios
KSSV
el 17 de Mayo de 2018
Do you have dates/ years in the data?
Wendy Cameron
el 17 de Mayo de 2018
Respuesta aceptada
Más respuestas (3)
Ameer Hamza
el 17 de Mayo de 2018
One way to do this is follow
[group, uniqueYears] = findgroups(A.years)
yearSum = splitapply(@sum, A.Temp, group)
yearSummary = [uniqueYears, group];
Or you can also use accumarray as follow:
yearSum = accumarray(A.years, A.Temp);
1 comentario
Wendy Cameron
el 17 de Mayo de 2018
Andrei Bobrov
el 17 de Mayo de 2018
data = readtable('Reset Accum_temp.xls','range','A2:B32');
[~,~,c] = unique(data.Year);
N = accumarray(c,data.Temperature);
T = data.Temperature;
lo = [0;diff(data.Year)]~=0;
T(lo) = T(lo) - N(1:end-1);
data.AccumulatedTemperatureOfEachYear = cumsum(T);
1 comentario
Wendy Cameron
el 17 de Mayo de 2018
Razvan Carbunescu
el 17 de Mayo de 2018
If using R2018a and wanting the final sum each year only can use groupsummary to get this more directly:
>> T = readtable('Reset Accum_temp.xls','range','A2:B32');
>> GT = groupsummary(T,'Year','sum','Temperature')
GT =
3×3 table
Year GroupCount sum_Temperature
____ __________ _______________
2015 10 267
2016 10 166
2017 10 208
You can use the date directly also with groupsummary
>> T.Date = datetime(T.Year,1,1); % reconstruct full Date
>> GT = groupsummary(T,'Date','year','sum','Temperature')
GT =
3×3 table
year_Date GroupCount sum_Temperature
_________ __________ _______________
2015 10 267
2016 10 166
2017 10 208
1 comentario
Wendy Cameron
el 18 de Mayo de 2018
Categorías
Más información sobre Time Series Objects 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!