how to create an index that keep tracking the hours in the data

3 Ansichten (letzte 30 Tage)
Is anyone helping me with a simple code ???
In the code below I want to find the peak load and average peak price in peak hour from 7-10 and then do it the same for each peak hours (11-15, 16-21 and 23-6am. I write the code below but I couldn't figure out how to calculate the peak load and average peak price for each peak with the same code
h=twomonth(:,1)
p1= 7 <= h & h < 10
price=twomonth(:,2);
price=price.*p1
demand=twomonth(:,3);
demand=demand.*p1
Index1=(7:24:N)'; ss1=10-1
N1=length( Index1);
Data1=zeros(N1,2);
for k=1:N1
kk=Index1(k); kkk=kk+ss1-1;
dd=sum(d(kk:kkk));pp=sum(p(kk:kkk).*d(kk:kkk)); pp=pp/dd;
Data1(k,:)=[pp dd];
end
  2 Kommentare
Star Strider
Star Strider am 30 Aug. 2019
Bearbeitet: Star Strider am 30 Aug. 2019
What does ‘isn’t working’ mean when you try to run it?
I can’t run it.
The error:
d=load.*p1
Error using load
Unable to read file 'matlab.mat'. No such file or directory.
Please do not name a variable ‘load’. It overshadows the load function, necessary for reading .mat files and other files.
What do you want to do in that line? Note that you are overwriting ‘d’ that you defined in the line just before it.
Also, ’p’, and ‘N’ are not defined.
gjashta
gjashta am 30 Aug. 2019
Sorry Star Strider, you are right! I have fixed my mistakes in the code above.
Hope I was clear enough and you can help me!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 2 Sep. 2019
Bearbeitet: Guillaume am 2 Sep. 2019
Here's how I'd do it:
%twomonth: a Nx3 matrix, whose first column is hour, 2nd is price, 3rd is demand
bin = discretize(mod(twomonth(:, 1)-7, 24), [7, 11, 16, 22, 23, Inf]-7); %offset bin edges by 7 in order to put the 23-6 in the same bin (5)
%bin == 1 is [7-11), bin == 2 is [11-16), bin == 3 is [16-22), bin == 4 is [22], bin == 5 is [23-6]
avgprice = splitapply(@mean, twomonth(:, 2), bin);
peakdemand = splitapply(@max, twomonth(:, 3), bin);
result = table({'7-10'; '11-15'; '16-21'; '22'; '23-6'}, avgprice, peakdemand, 'VariableNames', {'period', 'avgprice', 'peakdemand'})
I've included the lone 22 in the result, you can always remove row 4 if you don't want it.
You have to do a little bit of arithmetic as discretize can't give you the same bin number for the start and end of a range, but once you've got your bin numbers it's trivial to calculate your mean and peak with splitapply.
  14 Kommentare
Guillaume
Guillaume am 3 Sep. 2019
Oh yes, starthour probably needs transposing
result = table(groupday, starthour.', avgprice, peakdemand, weightedprice)
if it's not that then please give the size of each variables.
gjashta
gjashta am 3 Sep. 2019
Thank you very much Guillaume! Now the code is OK!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by