Calculate mean value between 2 years
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a set of data from Jan 2000 to 2011, and I want to calculate mean value of data from Nov of one year to Apr of the next year. How can I do it?
0 commentaires
Réponse acceptée
Simon Chan
le 30 Mar 2022
Modifié(e) : Simon Chan
le 30 Mar 2022
Not sure what is the format of your data. Use function isbetween and below shows an example that you can implement it into your code.
TimeStamps = datetime([2000 11 4; 2001 3 2; 2001 8 15; 2001 10 10;...
2001 12 14; 2002 1 31; 2002 3 25;...
2002 4 29; 2002 12 21; 2003 3 18]);
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TT = timetable(TimeStamps,Profit);
tlower = datetime(2000,11,1)+calyears(0:11);
thigher = datetime(2001,4,30)+calyears(0:11);
meanProfit = arrayfun(@(x,y) mean(TT.Profit(isbetween(TT.TimeStamps,x,y))),tlower,thigher)
3 commentaires
Simon Chan
le 31 Mar 2022
Do you want the following output?
Or do you want to use function timerange?
TimeStamps = datetime([2000 11 4; 2001 3 2; 2001 8 15; 2001 10 10;...
2001 12 14; 2002 1 31; 2002 3 25;...
2002 4 29; 2002 12 21; 2003 3 18]);
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TT = timetable(TimeStamps,Profit);
tlower = datetime(2000,11,1)+calyears(0:11);
thigher = datetime(2001,4,30)+calyears(0:11);
meanProfit = arrayfun(@(x,y) mean(TT.Profit(isbetween(TT.TimeStamps,x,y))),tlower,thigher);
table(tlower',thigher',meanProfit','VariableName',{'Period Start','Period End','Mean'})
Plus de réponses (1)
Sam Chak
le 30 Mar 2022
I think the M = mean(A) function will do the job. Give it a try.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!