How to create a script in Matlab
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following codes: I want to bin the price data in 4 bins per month and to calculate the mean and std of demand that belong in each bin by month and year.
Can you help me to create a function that contain (the bin, month, year) as a script?? I am repeating the same code for each month, I need a structure that calculate
them very fast and save all the results in one table.
Price = Data1(:,1 );
Demand = Data1(:,2 );
tv = (datetime([2006,1,1]):datetime([2018,12,31])).';
[Ye,M,D] = ymd(tv);
DT = timetable(Price,Demand, Ye, M, D, 'RowTimes', tv );
MarchDataPD = DT(DT.M==3,{'Price','Demand'});
topEdge1=max(P.Price(1:31));
botEdge1=min(P.Price(1:31));
NumBins=4;
binEdges1=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(1:31),binEdges1);
for i = 1:NumBins
flagBinMembers=(WhichBin==i);
binMembers=d.Demand(flagBinMembers);
binMean1(i)=mean(binMembers);
binstd1(i)=std(binMembers);
end
topEdge2=max(P.Price(32:62));
botEdge2=min(P.Price(32:62));
NumBins=4;
binEdges2=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(32:62),binEdges2);
for s = 1:NumBins
flagBinMembers=(WhichBin==s);
binMembers=d.Demand(flagBinMembers);
binMean2(s)=mean(binMembers);
binstd2(s)=std(binMembers);
end
0 commentaires
Réponse acceptée
Guillaume
le 11 Mai 2019
It's a bit puzzling that you are aware of timetables and yet resort to deprecated functions such as histc (which has no concept of dates) for the rest of your code, instead of using timetables functions which would make your task trivially easy.
Even the way you construct your timetable is more complicated than it should:
%Data1: a 2 column matrix of price and demand:
DT = array2timetable(Data1, 'VariableNames', {'Price', 'Demand'}, 'RowTimes', datetime(2006, 1, 1):dateime(2018, 12, 31));
There is no point in storing Ye, M, D separately. It's already stored as part of the time.
Once you have a timetable, you can easily resample it at regular or irregular intervals using retime. However retime doesn't do
%DT: a timetable
meanDT = retime(DT, 'regular', 'mean', 'TimeStep', caldays(14)); %discussion about timestep coming up
stdDT = retime(DT, 'regular', @std, 'TimeStep', caldays(14));
meanDT.Properties.VariableNames = {'MeanPrice', 'MeanDemand'};
stdDT.Properties.VariableNames = {'StdPrice', 'StdDemand'};
meanstdDT = [meanDT, stdDT]
meanstdDT = groupsummary(DT, 'Time', caldays(14), {'mean', 'std'})
Now you say you want 4 bins per month. What does that mean exactly. 4 equal bins per month means that the edges fall at odd times during the days instead of falling at the end of days. That doesn't sound like something you'd want. In the above, I've used bins of 14 days but both retime and groupsummary support a bin vector instead. To construct a bin vector with 4 equal bins per month, the simplest is probably:
monthvector = DT.Time(1) : calmonths(1) : DT.Time(end) + calmonths(1);
binvector = fillmissing(reshape([monthvector; NaT(3, numel(monthvector))], [], 1), 'linear')
and then
meanstd = groupsummary(DT, 'Time', binvector, {'mean', 'std'})
5 commentaires
Guillaume
le 14 Mai 2019
So, I need a better binning way to have reasonable observations that belong to 4 bins of the month
Well, you're free to use whichever binning method you want. As I've no idea why you're doing this, I've nothing to suggest. Perhaps you shoud bin according to demand instead of price but the binning method is for you to decide.
In the end, whichever binning method you'd still pass the bin edges to groupsummary.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Timetables 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!