Find timeseries mean for 10 years

Hi,
I have time-series temperature data for years 1950 to 2010.
I want to calculate the decadal means i.e. 1950 - 1960, 1960 - 1970 etc and plot them as a histogram
What code should I use to calculate the 10 year means?
Thank you.

 Risposta accettata

Adam Danz
Adam Danz il 15 Dic 2019
Modificato: Adam Danz il 15 Dic 2019
Assuming you have a vector named temperature that stores the temperatures and a vector named dt that stores the datetime stamps (in datetime format), you can use findgroups() to group the data into decades and then splitapply() to calculate the group means.
% Create demo data
dt = datetime(1950,1,1):datetime(2019,12,15);
temperature = rand(size(dt)) * 100;
% Group by decade
[groups, groupID] = findgroups(floor(year(dt)/10)*10);
% Compute decade means
decMeans = splitapply(@mean,temperature,groups);
% Put results into a table
results = table(groupID.', decMeans.','VariableNames',{'Decade','MeanTemp'});
View results
results =
7×2 table
Decade MeanTemp
______ ________
1950 49.348
1960 50.959
1970 50.13
1980 49.847
1990 49.808
2000 49.471
2010 49.404
[update]
If there are NaN values you'd like to ignore,
decMeans = splitapply(@(x)mean(x,'omitnan'),temperature,groups);

6 Commenti

Sarah Yun
Sarah Yun il 16 Dic 2019
Modificato: Sarah Yun il 16 Dic 2019
Hello,
What if the data is listed in years already? (there is no month value for the 'year' function below to assign 1, 2, 3, etc)?
i.e.
1978
1979
1980
1801
How should I change the code to make it work for single year increases?
dt = Year; % yearly increments
% How should the next line change for only yearly increases - no months?
[groups, groupID] = findgroups(floor(year(dt)/10)*10);
% Compute decade means and ignore NAN values
decMeans = splitapply(@(x)mean(x,'omitnan'),Precip,groups);
% Display results as a table
results = table(groupID.', decMeans.','VariableNames',{'Decade','MeanTemp'});
Thank you.
Adam Danz
Adam Danz il 16 Dic 2019
Modificato: Adam Danz il 16 Dic 2019
If your timestamps are years in a variable named 'y', and you want averages for every 10 years,
[groups, groupID] = findgroups(floor(y/10)*10);
and then you can use the rest of my answer.
If you want averages for every year,
[groups, groupID] = findgroups(y);
Sarah Yun
Sarah Yun il 16 Dic 2019
Modificato: Sarah Yun il 16 Dic 2019
Hi,
Thank you.
Now I want to run ANOVA test between the decade means
There are 9 decades, from 1900 to 2000 i.e. 1900 - 1910 etc and 9 mean values for each decade
I want to test for significance between these decades
I want to plot results (and p-values) on a histogram
How can I do this (I am a beginner, so nothing too complicated).
Thank you.
If you'd like to perform a 1-way analysis of variance between the temperatures grouped by decade, continuing from my answer above,
p = anova1(temperature(:),groups(:));
Thank you.
How do I find the p-values?
It shows a box-plot only.
Did you look at the p output?
Here's more info

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by