How to create a script in Matlab
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Timetables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!