Storing unique hour and minute values
Show older comments
I want the code below to runs through a large dataset to distinguish between weekend and weekday consumption at each hour and corresponding minute to calculate the average hourly use value (indicated by A). I tried several different ways to no previal. Any help would be much appreciated.
for i = 1:length(UniqueTimeSteps)
TimeValue(i) = UniqueTimeSteps(i);
[HourValue,MinValue,Seconds] = hms(UniqueTimeSteps(i));
idx = (t.Hour + t.Minute == HourValue & MinValue & weekday(t) > 1 & weekday(t) < 7);
Consumption_Weekday(i) = mean(A(idx));
idx = (t.Hour + t.Minute == HourValue & MinValue & (weekday(t) == 1 | weekday(t) == 7));
Consumption_Weekend(i) = mean(A(idx));
end
figureA = figure;
plot(TimeValue,Consumption_Weekday,'-*b');
hold on
plot(TimeValue,Consumption_Weekend,'--r');
8 Comments
Fatemah Ebrahim
on 11 May 2020
Mehmed Saad
on 11 May 2020
please attach complete code.
Fatemah Ebrahim
on 11 May 2020
Mehmed Saad
on 11 May 2020
Edited: Mehmed Saad
on 11 May 2020
so what you are trying to do is compute per hour cosumption of complete dataset (in weekdays and weekends)
Am i right?
Fatemah Ebrahim
on 11 May 2020
Walter Roberson
on 11 May 2020
That code is super inefficient.
idx = (t.Minute == MinValue & weekday(t) > 1 & weekday(t) < 7);
That matches all of t to find the entries that have the same Minute (of any hour) as the current unique time, and are Monday to Friday. But the same minute occurs in many different entries, and you are doing the same calculation over and over for all of them.
You should be looping over the unique minute entries. Or, more likely, you should be looping over minutes 0 to 59 without bothering to look for unique entries.
You should also be pre-allocating your output arrays.
I really recommend that you investigate findgroups() and splitapply() or grpstats()
Fatemah Ebrahim
on 11 May 2020
Mehmed Saad
on 11 May 2020
I agree with walter. you need to re write the code.
Accepted Answer
More Answers (1)
Walter Roberson
on 11 May 2020
No. Your code really needs a significant rewrite.
To run the same code multiple times, use a loop; in particular write a function that accepts a file name and does the work for you and returns the result. Then you only need one copy of the code.
t = readtable('A_minute.xlsx');
G = findgroups(minute(t.Var1), isweekend(t.Var1));
output = grpstats(t.P4_kWh', G, 'mean');
Consumption_Weekday = output(1:2:end,:);
Consumption_Weekend = output(2:2:end,:);
plot([Consumption_Weekday, Consumption_Weekend]);
legend({'Weekday', 'Weekend'})
Categories
Find more on Downloads 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!