Calculate the maximum and minimum values and what day these values occured
12 次查看(过去 30 天)
显示 更早的评论
Hello I have a big timetable (one year) with temperature recorded every minute. I want to calculate the maximum and minimum values of every month and what day these values occured.
x=timetable(date_time, Var);
MaxTemp = retime(x,'monthly','max');
MinTemp = retime(x,'monthly','min');
Any ideas how to proceed?
1 个评论
othman warde
2023-2-14
编辑:Walter Roberson
2023-2-14
To determine the maximum and minimum values of every month and the corresponding dates on which these values occurred, you can use the resample function in MATLAB. Here's an example of how you can use it:.
% Convert the timetable to a timetable with monthly intervals
monthlyData = retime(x, 'monthly', 'mean');
% Extract the maximum and minimum values of each month
MaxTemp = resample(x.Var, 'monthly', 'max');
MinTemp = resample(x.Var, 'monthly', 'min');
% Find the dates corresponding to the maximum and minimum values
MaxTempDates = resample(x.date_time, 'monthly', @(x) x(find(x.Var == max(x.Var), 1))); MinTempDates = resample(x.date_time, 'monthly', @(x) x(find(x.Var == min(x.Var), 1)));
In the code above, retime is used to convert the original timetable to a new timetable with monthly intervals. Then, resample is used to extract the maximum and minimum values of each month. Finally, resample is used again to find the dates corresponding to the maximum and minimum values. The @(x) x(find(x.Var == max(x.Var), 1)) and @(x) x(find(x.Var == min(x.Var), 1)) functions are used to find the first occurrence of the maximum and minimum values in each month, respectively. The resulting MaxTempDates and MinTempDates variables will be timetables with the date of the maximum and minimum values for each month.
采纳的回答
Star Strider
2023-1-6
That dealt with the hour that the daily maximum occurred. It would be straightforward to change that code to do what you want to do here.
44 个评论
Star Strider
2023-1-6
It took a bit longer than I anticipated to adapt my previous code to this problem.
Try this —
DT = datetime('01-Dec-2022 00:00:00') + days(0:365).';
Temperature = sin(2*pi*(0:numel(DT)-1)/24-0.5).'*12+5 + randn(size(DT))*1.5;
TT1 = timetable(DT, Temperature)
TT1 = 366×1 timetable
DT Temperature
___________ ___________
01-Dec-2022 0.66504
02-Dec-2022 1.5196
03-Dec-2022 5.691
04-Dec-2022 10.741
05-Dec-2022 10.712
06-Dec-2022 11.851
07-Dec-2022 15.96
08-Dec-2022 19.993
09-Dec-2022 17.681
10-Dec-2022 16.038
11-Dec-2022 15.199
12-Dec-2022 10.901
13-Dec-2022 13.714
14-Dec-2022 10.91
15-Dec-2022 3.3633
16-Dec-2022 2.7038
figure
plot(TT1.DT, TT1.Temperature)
grid
MonthlyMax = retime(TT1, 'monthly', 'max')
MonthlyMax = 13×1 timetable
DT Temperature
___________ ___________
01-Dec-2022 19.993
01-Jan-2023 18.335
01-Feb-2023 18.99
01-Mar-2023 17.149
01-Apr-2023 18.211
01-May-2023 20.026
01-Jun-2023 17.665
01-Jul-2023 20.068
01-Aug-2023 18.648
01-Sep-2023 17.956
01-Oct-2023 16.865
01-Nov-2023 19.498
01-Dec-2023 14.852
DailyMax = retime(TT1, 'daily', 'max')
DailyMax = 366×1 timetable
DT Temperature
___________ ___________
01-Dec-2022 0.66504
02-Dec-2022 1.5196
03-Dec-2022 5.691
04-Dec-2022 10.741
05-Dec-2022 10.712
06-Dec-2022 11.851
07-Dec-2022 15.96
08-Dec-2022 19.993
09-Dec-2022 17.681
10-Dec-2022 16.038
11-Dec-2022 15.199
12-Dec-2022 10.901
13-Dec-2022 13.714
14-Dec-2022 10.91
15-Dec-2022 3.3633
16-Dec-2022 2.7038
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthlySTD = retime(TT1, 'monthly', @std);
T2 = table(MonthlyMax.DT, MonthlyMax.Temperature, MonthlyMin.Temperature, MonthlySTD.Temperature, 'VariableNames',{'DT','Max','Min','StDv'})
T2 = 13×4 table
DT Max Min StDv
___________ ______ _______ ______
01-Dec-2022 19.993 -7.0232 8.0264
01-Jan-2023 18.335 -8.7917 8.6537
01-Feb-2023 18.99 -8.032 8.3328
01-Mar-2023 17.149 -7.717 8.5402
01-Apr-2023 18.211 -8.5859 8.0011
01-May-2023 20.026 -9.9484 8.9743
01-Jun-2023 17.665 -7.8864 8.6088
01-Jul-2023 20.068 -8.1143 8.6465
01-Aug-2023 18.648 -6.5155 8.789
01-Sep-2023 17.956 -8.7416 8.7519
01-Oct-2023 16.865 -9.2885 9.0736
01-Nov-2023 19.498 -8.6919 8.5491
01-Dec-2023 14.852 14.852 0
Thresholdv = [16; 17; 18; 19; 20]; % Column Vector
for k = 1:numel(Thresholdv)
DaysMeetingThreshold(k,:) = nnz(T2.Max >= Thresholdv(k)); % Days Equal To Or Higher Than Thresholdv (°C)
end
ThresholdTable = table(Thresholdv, DaysMeetingThreshold)
ThresholdTable = 5×2 table
Thresholdv DaysMeetingThreshold
__________ ____________________
16 12
17 11
18 8
19 4
20 2
[yd,md,dd] = ymd(DailyMax.DT); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyMax)
[ym,mm,dm] = ymd(MonthlyMax.DT(k));
Lv = ismember([yd,md], [ym,mm], 'rows');
idx = DailyMax.Temperature(Lv) == MonthlyMax.Temperature(k);
Mv = find(Lv);
Dv = find(idx);
HM = DailyMax(Mv(Dv),:);
MonthDayMax(k,:) = timetable2table(HM);
end
MonthDayMax % Table Of Day In Each Month At Which Maximum Temperature Occurred
MonthDayMax = 13×2 table
DT Temperature
___________ ___________
08-Dec-2022 19.993
03-Jan-2023 18.335
18-Feb-2023 18.99
15-Mar-2023 17.149
08-Apr-2023 18.211
01-May-2023 20.026
18-Jun-2023 17.665
11-Jul-2023 20.068
29-Aug-2023 18.648
22-Sep-2023 17.956
18-Oct-2023 16.865
11-Nov-2023 19.498
01-Dec-2023 14.852
.
Ancalagon8
2023-1-6
编辑:Ancalagon8
2023-1-6
I still receive the error:
"To assign to or create a variable in a table, the number of rows must match the height of the table."
Your code with these variables (DT,Temperature,TT1) works fine, but with my dataset no..
Star Strider
2023-1-6
There were duplicate maxima, and that caused the problem.
Changing this assignment to only select the first index:
HM = DailyMax(Mv(Dv(1)),:);
solved the problem.
I initially forgot the corresponding minima, so I added a separate loop for that. It is simply a duplicate of the first loop with appropriate changes.
Bothe the maxima and minima are now calculated.
% DT = datetime('01-Dec-2022 00:00:00') + days(0:365).';
% Temperature = sin(2*pi*(0:numel(DT)-1)/24-0.5).'*12+5 + randn(size(DT))*1.5;
% TT1 = timetable(DT, Temperature)
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'))
LD = struct with fields:
TT1: [513077×1 timetable]
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
figure
plot(TT1.date_time, TT1.Temperature)
grid
% return
MonthlyMax = retime(TT1, 'monthly', 'max')
MonthlyMax = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.241
01-Feb-19 00:00:00 10.278
01-Mar-19 00:00:00 10.269
01-Apr-19 00:00:00 10.261
01-May-19 00:00:00 10.204
01-Jun-19 00:00:00 10.204
01-Jul-19 00:00:00 10.184
01-Aug-19 00:00:00 10.175
01-Sep-19 00:00:00 10.223
01-Oct-19 00:00:00 10.231
01-Nov-19 00:00:00 10.212
01-Dec-19 00:00:00 10.292
DailyMax = retime(TT1, 'daily', 'max')
DailyMax = 365×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.18
02-Jan-19 00:00:00 10.174
03-Jan-19 00:00:00 10.139
04-Jan-19 00:00:00 10.182
05-Jan-19 00:00:00 10.166
06-Jan-19 00:00:00 10.162
07-Jan-19 00:00:00 10.221
08-Jan-19 00:00:00 10.233
09-Jan-19 00:00:00 10.184
10-Jan-19 00:00:00 10.109
11-Jan-19 00:00:00 10.152
12-Jan-19 00:00:00 10.119
13-Jan-19 00:00:00 10.12
14-Jan-19 00:00:00 10.084
15-Jan-19 00:00:00 10.189
16-Jan-19 00:00:00 10.241
DailyMin = retime(TT1, 'daily', 'min')
DailyMin = 365×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.134
02-Jan-19 00:00:00 10.083
03-Jan-19 00:00:00 10.05
04-Jan-19 00:00:00 10.137
05-Jan-19 00:00:00 10.138
06-Jan-19 00:00:00 10.122
07-Jan-19 00:00:00 10.161
08-Jan-19 00:00:00 10.183
09-Jan-19 00:00:00 10.006
10-Jan-19 00:00:00 10.01
11-Jan-19 00:00:00 10.109
12-Jan-19 00:00:00 10.077
13-Jan-19 00:00:00 10.084
14-Jan-19 00:00:00 10.002
15-Jan-19 00:00:00 10.006
16-Jan-19 00:00:00 10.19
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthlySTD = retime(TT1, 'monthly', @std);
T2 = table(MonthlyMax.date_time, MonthlyMax.Temperature, MonthlyMin.Temperature, MonthlySTD.Temperature, 'VariableNames',{'date_time','Max','Min','StDv'})
T2 = 12×4 table
date_time Max Min StDv
__________________ ______ ______ ________
01-Jan-19 00:00:00 10.241 9.8976 NaN
01-Feb-19 00:00:00 10.278 10.053 0.049242
01-Mar-19 00:00:00 10.269 10.033 0.047355
01-Apr-19 00:00:00 10.261 10.027 0.052837
01-May-19 00:00:00 10.204 10.006 0.033589
01-Jun-19 00:00:00 10.204 10.051 0.024046
01-Jul-19 00:00:00 10.184 10.035 0.033762
01-Aug-19 00:00:00 10.175 10.061 0.027458
01-Sep-19 00:00:00 10.223 10.087 0.031658
01-Oct-19 00:00:00 10.231 10.041 0.043407
01-Nov-19 00:00:00 10.212 10.047 NaN
01-Dec-19 00:00:00 10.292 9.9079 NaN
Thresholdv = [16; 17; 18; 19; 20]; % Column Vector
for k = 1:numel(Thresholdv)
DaysMeetingThreshold(k,:) = nnz(T2.Max >= Thresholdv(k)); % Days Equal To Or Higher Than Thresholdv (°C)
end
ThresholdTable = table(Thresholdv, DaysMeetingThreshold)
ThresholdTable = 5×2 table
Thresholdv DaysMeetingThreshold
__________ ____________________
16 0
17 0
18 0
19 0
20 0
[ydx,mdx,ddx] = ymd(DailyMax.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyMax)
[ym,mm,dm] = ymd(MonthlyMax.date_time(k));
Lv = ismember([ydx,mdx], [ym,mm], 'rows');
idx = DailyMax.Temperature(Lv) == MonthlyMax.Temperature(k);
Mv = find(Lv);
Dv = find(idx);
HM = DailyMax(Mv(Dv(1)),:);
MonthDayMax(k,:) = timetable2table(HM);
end
[ydn,mdn,ddn] = ymd(DailyMin.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyMin)
[ym,mm,dm] = ymd(MonthlyMin.date_time(k));
Lv = ismember([ydn,mdn], [ym,mm], 'rows');
idx = DailyMin.Temperature(Lv) == MonthlyMin.Temperature(k);
Mv = find(Lv);
Dv = find(idx);
HM = DailyMin(Mv(Dv(1)),:);
MonthDayMin(k,:) = timetable2table(HM);
end
MonthDayMax % Table Of Day In Each Month At Which Maximum Temperature Occurred
MonthDayMax = 12×2 table
date_time Temperature
__________________ ___________
16-Jan-19 00:00:00 10.241
17-Feb-19 00:00:00 10.278
09-Mar-19 00:00:00 10.269
21-Apr-19 00:00:00 10.261
30-May-19 00:00:00 10.204
07-Jun-19 00:00:00 10.204
01-Jul-19 00:00:00 10.184
20-Aug-19 00:00:00 10.175
15-Sep-19 00:00:00 10.223
12-Oct-19 00:00:00 10.231
16-Nov-19 00:00:00 10.212
18-Dec-19 00:00:00 10.292
MonthDayMin % Table Of Day In Each Month At Which Minimum Temperature Occurred
MonthDayMin = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 00:00:00 9.8976
06-Feb-19 00:00:00 10.053
12-Mar-19 00:00:00 10.033
06-Apr-19 00:00:00 10.027
05-May-19 00:00:00 10.006
29-Jun-19 00:00:00 10.051
10-Jul-19 00:00:00 10.035
03-Aug-19 00:00:00 10.061
05-Sep-19 00:00:00 10.087
04-Oct-19 00:00:00 10.041
26-Nov-19 00:00:00 10.047
14-Dec-19 00:00:00 9.9079
Are those the desired results?
.
Ancalagon8
2023-1-6
Yes! I receive the desired days but what about the hours? I notice everywhere 00:00:00.
Star Strider
2023-1-7
编辑:Star Strider
2023-1-7
I thought you only wanted the daily max and min.
This tweak gives the day and hour for the max and min each month —
% DT = datetime('01-Dec-2022 00:00:00') + days(0:365).';
% Temperature = sin(2*pi*(0:numel(DT)-1)/24-0.5).'*12+5 + randn(size(DT))*1.5;
% TT1 = timetable(DT, Temperature)
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'))
LD = struct with fields:
TT1: [513077×1 timetable]
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
figure
plot(TT1.date_time, TT1.Temperature)
grid
% return
MonthlyMax = retime(TT1, 'monthly', 'max')
MonthlyMax = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.241
01-Feb-19 00:00:00 10.278
01-Mar-19 00:00:00 10.269
01-Apr-19 00:00:00 10.261
01-May-19 00:00:00 10.204
01-Jun-19 00:00:00 10.204
01-Jul-19 00:00:00 10.184
01-Aug-19 00:00:00 10.175
01-Sep-19 00:00:00 10.223
01-Oct-19 00:00:00 10.231
01-Nov-19 00:00:00 10.212
01-Dec-19 00:00:00 10.292
MinutelyMax = retime(TT1, 'minutely', 'max')
MinutelyMax = 525600×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:01:00 NaN
01-Jan-19 00:02:00 NaN
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
MinutelyMin = retime(TT1, 'minutely', 'min')
MinutelyMin = 525600×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:01:00 NaN
01-Jan-19 00:02:00 NaN
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthlySTD = retime(TT1, 'monthly', @std);
T2 = table(MonthlyMax.date_time, MonthlyMax.Temperature, MonthlyMin.Temperature, MonthlySTD.Temperature, 'VariableNames',{'date_time','Max','Min','StDv'})
T2 = 12×4 table
date_time Max Min StDv
__________________ ______ ______ ________
01-Jan-19 00:00:00 10.241 9.8976 NaN
01-Feb-19 00:00:00 10.278 10.053 0.049242
01-Mar-19 00:00:00 10.269 10.033 0.047355
01-Apr-19 00:00:00 10.261 10.027 0.052837
01-May-19 00:00:00 10.204 10.006 0.033589
01-Jun-19 00:00:00 10.204 10.051 0.024046
01-Jul-19 00:00:00 10.184 10.035 0.033762
01-Aug-19 00:00:00 10.175 10.061 0.027458
01-Sep-19 00:00:00 10.223 10.087 0.031658
01-Oct-19 00:00:00 10.231 10.041 0.043407
01-Nov-19 00:00:00 10.212 10.047 NaN
01-Dec-19 00:00:00 10.292 9.9079 NaN
Thresholdv = [16; 17; 18; 19; 20]; % Column Vector
for k = 1:numel(Thresholdv)
DaysMeetingThreshold(k,:) = nnz(T2.Max >= Thresholdv(k)); % Days Equal To Or Higher Than Thresholdv (°C)
end
ThresholdTable = table(Thresholdv, DaysMeetingThreshold)
ThresholdTable = 5×2 table
Thresholdv DaysMeetingThreshold
__________ ____________________
16 0
17 0
18 0
19 0
20 0
[yhx,mhx,dhx] = ymd(MinutelyMax.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyMax)
[ym,mm,dm] = ymd(MonthlyMax.date_time(k));
Lv = ismember([yhx,mhx], [ym,mm], 'rows');
idx = MinutelyMax.Temperature(Lv) == MonthlyMax.Temperature(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyMax(Mv(Dv(1)),:);
MonthDayMax(k,:) = timetable2table(HM);
end
[yhn,mhn,dhn] = ymd(MinutelyMin.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyMin)
[ym,mm,dm] = ymd(MonthlyMin.date_time(k));
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyMin.Temperature(Lv) == MonthlyMin.Temperature(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyMin(Mv(Dv(1)),:);
MonthDayMin(k,:) = timetable2table(HM);
end
MonthDayMax % Table Of Day In Each Month At Which Maximum Temperature Occurred
MonthDayMax = 12×2 table
date_time Temperature
__________________ ___________
16-Jan-19 11:00:00 10.241
17-Feb-19 09:04:00 10.278
09-Mar-19 12:37:00 10.269
21-Apr-19 10:51:00 10.261
30-May-19 12:44:00 10.204
07-Jun-19 23:17:00 10.204
01-Jul-19 01:05:00 10.184
20-Aug-19 23:40:00 10.175
15-Sep-19 10:59:00 10.223
12-Oct-19 11:01:00 10.231
16-Nov-19 10:14:00 10.212
18-Dec-19 11:08:00 10.292
MonthDayMin % Table Of Day In Each Month At Which Minimum Temperature Occurred
MonthDayMin = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 05:25:00 9.8976
06-Feb-19 03:51:00 10.053
12-Mar-19 17:53:00 10.033
06-Apr-19 04:59:00 10.027
05-May-19 06:00:00 10.006
29-Jun-19 18:16:00 10.051
10-Jul-19 19:30:00 10.035
03-Aug-19 19:13:00 10.061
05-Sep-19 16:02:00 10.087
04-Oct-19 05:41:00 10.041
26-Nov-19 01:46:00 10.047
14-Dec-19 07:12:00 9.9079
I kept the previous code intact, and copied everything to here to tweak it.
EDIT — (7 Jan 2023 at 17:20)
Changed ‘Hourly’ to ‘Minutely’ to get accuracy to the minute.
.
Ancalagon8
2023-1-7
编辑:Ancalagon8
2023-1-7
As the initial sampling rate is per minute, the final results that calculate the hour can be further improved and camculate hour AND minute?
Ancalagon8
2023-1-8
Works fine again. What about calculating e.g. the 3 max values and the 3 min values? Will
retime(TT, 'minutely', 'max');
work?
Star Strider
2023-1-8
Thank you!
That depends on how ‘3 max values and the 3 min values’ are defined. For the entire year, use maxk and mink. The second output will be the indices, so to get the entire row, do something like this —
T = array2table(randn(10,2))
T = 10×2 table
Var1 Var2
_________ _________
0.034356 0.96346
-1.3018 2.0709
0.070568 -0.060068
0.67504 -1.4819
-1.0024 0.25872
-0.86488 1.3363
0.65376 0.51979
-0.093837 1.6005
-0.48581 -0.31631
0.77923 -0.74116
[Col1,idx] = maxk(T{:,1},3);
Tx3 = T(idx,:)
Tx3 = 3×2 table
Var1 Var2
_______ ________
0.77923 -0.74116
0.67504 -1.4819
0.65376 0.51979
With respect to tweaking my code to do something similar, that could be possible, however not without a significant amount of recoding. The problem would be how to define the output table or timetable. That’s not immmediately obvious to me. The retime function would not work for this, however maxk and mink likely could.
retime(TT, 'minutely', @(x)maxk(x,3));
However that wouldn’t work in practice. The reason is that according to the documentation in the method section Aggregation Methods with respect to funciton handle methods, ‘Use the function specified by the function handle (for example, @std to calculate the standard deviation for the values in each time bin). func must return an output argument that is a scalar or a row vector, and must accept empty inputs.’ (emphasis added). So returning the 3 maxima would likely throw an error.
One way around that could be to first back up the original timetable, then run the code on a copy of it to first find the maximum for each month, save that, delete it from the timetable, run the code again, save that, delete the maxima, and repeat as long as necessary to return the desired numbers of maxima (or minima). The result would be as many output tables or timetables as required to get the required number of maxima (or minima).
.
Ancalagon8
2023-1-25
@Star Strider in this part of the code,
Thresholdv = [16; 17; 18; 19; 20]; % Column Vector
for k = 1:numel(Thresholdv)
DaysMeetingThreshold(k,:) = nnz(T2.Max >= Thresholdv(k)); % Days Equal To Or Higher Than Thresholdv (°C)
end
ThresholdTable = table(Thresholdv, DaysMeetingThreshold)
DaysMeetingThreshold refer to the whole year as a sum of days. How can I split this result to receive a table showing the Threshold Temperatures per month?
Star Strider
2023-1-25
That was a shile ago and I’ve forgotten what we were doing with it.
Try something like this —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
figure
plot(TT1.date_time, TT1.Temperature)
grid
Thresholdv = (9.8:0.1:10.4).'; % Column Vector
for k1 = 1:12
Lv = month(TT1.date_time) == k1; % Select Month
Month = retime(TT1(Lv,:),'daily','mean'); % Daily Mean Temperature
MMM{k1,:} = month(TT1(Lv,:).date_time(1),'shortname'); % For Row Names
for k2 = 1:numel(Thresholdv)-1 % Histogram Loop
Tempv = Month.Temperature;
DaysMeetingThreshold(k1,k2) = nnz((Tempv >= Thresholdv(k2)) & (Tempv < Thresholdv(k2+1))); % Days Equal To Or Higher Than Thresholdv (°C)
end
end
DaysThresholdTable = array2table(DaysMeetingThreshold, 'VariableNames',compose('%.2f',Thresholdv(1:end-1)), 'RowNames',[MMM{:}])
DaysThresholdTable = 12×6 table
9.80 9.90 10.00 10.10 10.20 10.30
____ ____ _____ _____ _____ _____
Jan 0 3 11 14 3 0
Feb 0 0 1 14 13 0
Mar 0 0 2 21 8 0
Apr 0 0 5 20 5 0
May 0 0 3 28 0 0
Jun 0 0 0 30 0 0
Jul 0 0 11 20 0 0
Aug 0 0 6 25 0 0
Sep 0 0 0 25 5 0
Oct 0 0 3 20 8 0
Nov 0 0 5 25 0 0
Dec 0 2 6 10 13 0
This calculates a histogram of sorts without calling any histogram functions. I changed the code a bit to fit the actual data.
.
Ancalagon8
2023-1-27
Using:
DailyMax = retime(TT1, 'daily', 'max')
DailyMin = retime(TT1, 'daily', 'min')
I receive the daily range like this:
DailyRange=array@table(DailyMax-DailyMin);
Now how can I calculate monthly mean value? In other words for January to retrieve the mean value (first 32 values).Instead of doing it like this:
MeanJanuary=DailyRange(1:31,1);
is there a faster way?
Star Strider
2023-1-27
Perhaps —
MonthlyMean = retime(TT1,'monthly','mean')
That would seem to work (unless I am not understanding something).
Ancalagon8
2023-1-27
MonthlyMean = retime(TT1,'monthly','mean')
works but here i do not need the mean value, but the range (max - min).
Star Strider
2023-1-27
I thought you wanted the mean.
For that, probably:
MonthlyMax = retime(TT1, 'monthly', 'max')
MonthlyMin = retime(TT1, 'monthly', 'min')
Then do the monthly range calculation from those, similar to the ‘DailyRange’ calculation.
Ancalagon8
2023-1-27
编辑:Ancalagon8
2023-1-27
I have successfully calculated Monthly range (365 values). Instead of using retime, is there another way to split it into 12 monthly values? (So for January the MonthlyRange would be the first 31 values of Monthly range)
Star Strider
2023-1-27
I am a bit lost. The monthly range should be 12, not 365.
One way to do that would be to comkpare the month value in the ‘date_time’ variable with the months in a loop. Equate them, and then save those data to a different variable. Since I have no idea what you are doing at this point with the ‘MonthlyRange’ array, that is as precise as I can be. I hesitate to code with information I have no access to. There may be examples of that approach in code I have already written here.
.
Ancalagon8
2023-2-1
What I mean is that the goal here is to initially calculate for each day the Max and Min value, as well as Max-Min. So a 365X4 table should be exported, with date-time-hour-minute in first collumn, max in second, min in third and daily range (max-min) in the fourth collumn.
Then these 365 values should be splitted per month, in order to calculate (based on the fourth collumn with the daily ranges) for each month the mean value,the max value and the min value and when these values occured.
Maybe now is more clear!
Star Strider
2023-2-1
it seems that we did versions of that already. Isn’t it possible to just adapt those to the current problem? It would likely be necessary to do the timing separately for the max, min, range, and any others, each in different timetable arrays, similarly to the approach in my previous Comment.
They would of necessity each have to be different timetable arrays, because I seriously doubt that the same times would be applicable to the various max, min, range, and others. The way timetable arrays work, they could not be combined into one timetable, although it might be possible to put the verious dates, times, max, min, range, and other datetime-variable arrays in the same table, providing that they all had the same (e.g. 12) row lengths. That would have to be coded separately and they would first have to be copied to table arrays after all the timetable arrays were calcualted.
Ancalagon8
2023-2-1
I am trying for days to adapt the previous versions but obviously I miss something:
DailyMax = retime(TT1, 'daily', 'max');
DailyMin = retime(TT1, 'daily', 'min');
DailyRange = DailyMax.Temperature - DailyMin.Temperature;
DailyRange = timetable(DailyMax.date_time, DailyRange);
and then
RangeMax = retime(DailyRange, 'monthly', 'max')
RangeMin = retime(DailyRange, 'monthly', 'min')
RangeMean = retime(DailyRange, 'monthly', 'mean')
these are the results that I want, but I do not have the time information of RangeMax,RangeMin,RangeMean.
Star Strider
2023-2-1
I’m even more impressed with the table and timetable array types than I was previously!
In order to make it easier to get the exact times of the various maxima, minima, and the rest, I created a function ‘MonthlyToMinutely’ for that. It takes the 'monthly' and 'minutely' aggretated timetable arrays as arguments, and then returns a timetable with the exact times during the year that those values occurred in each month. It also checks to be certain that the timetable arguments have the same variables. It only works with timetable arrays with one variable, however that is what we are currently dealing with.
Here is an example of it with the 'min' timetable arrays —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
MonthDayMin = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 05:25:00 9.8976
06-Feb-19 03:51:00 10.053
12-Mar-19 17:53:00 10.033
06-Apr-19 04:59:00 10.027
05-May-19 06:00:00 10.006
29-Jun-19 18:16:00 10.051
10-Jul-19 19:30:00 10.035
03-Aug-19 19:13:00 10.061
05-Sep-19 16:02:00 10.087
04-Oct-19 05:41:00 10.041
26-Nov-19 01:46:00 10.047
14-Dec-19 07:12:00 9.9079
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
I did not test it with the others, however it should work with them.
.
Ancalagon8
2023-2-2
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
Unrecognized function or variable 'MonthlyToMinutely'.
Star Strider
2023-2-2
Either save the function to a file called ‘MonthlyToMinutely.m’ (that is likely the best approach, and save it to a subdirectory on your MATLAB search path), or put it at the end of the script you are using it with, similar to what I did here. Saving it to a file is likely the easiest solution.
The code you would save to the ‘MonthlyToMinutely.m’ file is:
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
.
Ancalagon8
2023-2-2
Thanks for your answer, the example with the 'min' timetable arrays works like a charm.
I went on coding the 'max' timetable arrays
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
but MonthDayMax is the same with MonthDayMin. Is there something to change in the function, in order to retrieve the right values of MonthDayMax?
Star Strider
2023-2-2
They are not the same when I run it —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
MonthDayMax = 12×2 table
date_time Temperature
__________________ ___________
16-Jan-19 11:00:00 10.241
17-Feb-19 09:04:00 10.278
09-Mar-19 12:37:00 10.269
21-Apr-19 10:51:00 10.261
30-May-19 12:44:00 10.204
07-Jun-19 23:17:00 10.204
01-Jul-19 01:05:00 10.184
20-Aug-19 23:40:00 10.175
15-Sep-19 10:59:00 10.223
12-Oct-19 11:01:00 10.231
16-Nov-19 10:14:00 10.212
18-Dec-19 11:08:00 10.292
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
MonthDayMin = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 05:25:00 9.8976
06-Feb-19 03:51:00 10.053
12-Mar-19 17:53:00 10.033
06-Apr-19 04:59:00 10.027
05-May-19 06:00:00 10.006
29-Jun-19 18:16:00 10.051
10-Jul-19 19:30:00 10.035
03-Aug-19 19:13:00 10.061
05-Sep-19 16:02:00 10.087
04-Oct-19 05:41:00 10.041
26-Nov-19 01:46:00 10.047
14-Dec-19 07:12:00 9.9079
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
MonthDayMax = 12×2 table
date_time Temperature
__________________ ___________
16-Jan-19 11:00:00 10.241
17-Feb-19 09:04:00 10.278
09-Mar-19 12:37:00 10.269
21-Apr-19 10:51:00 10.261
30-May-19 12:44:00 10.204
07-Jun-19 23:17:00 10.204
01-Jul-19 01:05:00 10.184
20-Aug-19 23:40:00 10.175
15-Sep-19 10:59:00 10.223
12-Oct-19 11:01:00 10.231
16-Nov-19 10:14:00 10.212
18-Dec-19 11:08:00 10.292
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
MonthDayMin = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 05:25:00 9.8976
06-Feb-19 03:51:00 10.053
12-Mar-19 17:53:00 10.033
06-Apr-19 04:59:00 10.027
05-May-19 06:00:00 10.006
29-Jun-19 18:16:00 10.051
10-Jul-19 19:30:00 10.035
03-Aug-19 19:13:00 10.061
05-Sep-19 16:02:00 10.087
04-Oct-19 05:41:00 10.041
26-Nov-19 01:46:00 10.047
14-Dec-19 07:12:00 9.9079
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
The function appears to work correctly.
.
Ancalagon8
2023-2-2
编辑:Ancalagon8
2023-2-2
Indeed works. As the results that I want were the RangeMax, RangeMin and RangeMean, I had to replace MonthlyMin/Max:
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
with DailyMin/Μax:
MinutelyMax = retime(TT1, 'minutely', 'max');
DailyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(DailyMax,MinutelyMax)
MinutelyMin = retime(TT1, 'minutely', 'min');
DailyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(DailyMin,MinutelyMin)
in order to retrieve MonthDayMax (365X2 table) MonthDayMin (365X2 table).
So now I continued like this:
DailyRange = MonthDayMax.Temperature - MonthDayMin.Temperature;
DailyRange = timetable(MonthDayMin.date_time, DailyRange);
RangeMean = retime(DailyRange, 'monthly', 'mean')
RangeMax = retime(DailyRange, 'monthly', 'max')
RangeMin = retime(DailyRange, 'monthly', 'min')
The thing is that the retrieved MonthDayMax (365X2) and MonthDayMin (365X2) tables are wrong, as there is not a unique daily max or min value:
MonthDayMin =
365×2 table
date_time Temperature
__________________ ___________
01-Jan-19 23:44:00 8.093
02-Jan-19 08:02:00 3.485
03-Jan-19 23:41:00 5.057
04-Jan-19 09:42:00 2.584
05-Jan-19 00:49:00 2.325
06-Jan-19 07:31:00 0.578
05-Jan-19 00:33:00 2.546
08-Jan-19 08:20:00 -0.452
04-Jan-19 02:38:00 4.652
01-Jan-19 10:17:00 10.299
02-Jan-19 00:03:00 7.933
02-Jan-19 10:33:00 8.475
02-Jan-19 08:26:00 3.927
02-Jan-19 07:02:00 5.179
02-Jan-19 03:16:00 6.674
08-Jan-19 03:37:00 2.21
04-Jan-19 05:44:00 3.393
02-Jan-19 00:07:00 7.941
01-Jan-19 03:32:00 10.1
02-Jan-19 00:25:00 8.002
03-Jan-19 04:47:00 6.445
01-Jan-19 21:03:00 9.032
01-Jan-19 00:17:00 9.826
02-Jan-19 09:37:00 7.62
01-Jan-19 22:08:00 8.567
02-Jan-19 02:47:00 7.109
03-Jan-19 05:44:00 6.064
01-Jan-19 18:19:00 9.284
01-Jan-19 03:08:00 10.108
02-Jan-19 13:03:00 9.482
02-Jan-19 13:06:00 9.513
01-Feb-19 02:21:00 8.01
01-Feb-19 20:46:00 14.091
02-Feb-19 11:07:00 16.502
01-Feb-19 11:19:00 14.434
05-Feb-19 01:19:00 13.572
01-Feb-19 09:29:00 11.878
07-Feb-19 05:00:00 9.932
08-Feb-19 06:29:00 6.369
09-Feb-19 06:03:00 6.72
10-Feb-19 07:10:00 6.682
01-Feb-19 00:55:00 9.291
01-Feb-19 01:11:00 9.07
08-Feb-19 08:47:00 8.193
01-Feb-19 03:59:00 8.452
15-Feb-19 07:44:00 5.804
16-Feb-19 02:28:00 5.27
17-Feb-19 06:42:00 3.943
18-Feb-19 07:52:00 5.781
08-Feb-19 03:50:00 7.33
08-Feb-19 01:42:00 7.88
08-Feb-19 06:06:00 7.399
09-Feb-19 07:15:00 7.056
15-Feb-19 07:41:00 6.01
24-Feb-19 07:02:00 3.828
17-Feb-19 05:47:00 5.072
15-Feb-19 07:02:00 6.636
09-Feb-19 02:35:00 7.933
16-Feb-19 02:26:00 5.53
.......
.......
01-Nov-19 07:17:00 14.61
01-Nov-19 01:33:00 16.09
01-Nov-19 08:22:00 17.364
01-Nov-19 00:33:00 16.632
01-Nov-19 00:36:00 16.494
01-Nov-19 00:00:00 16.899
01-Nov-19 06:47:00 15.083
01-Nov-19 00:46:00 16.365
01-Nov-19 06:59:00 14.892
10-Nov-19 23:59:00 14.48
11-Nov-19 07:18:00 13.114
01-Nov-19 05:57:00 14.961
09-Nov-19 02:47:00 15.052
11-Nov-19 05:44:00 13.732
11-Nov-19 05:33:00 13.809
01-Nov-19 06:36:00 15.419
01-Nov-19 01:18:00 16.357
11-Nov-19 05:33:00 13.809
11-Nov-19 05:36:00 13.694
10-Nov-19 22:01:00 14.846
21-Nov-19 07:23:00 12.42
21-Nov-19 06:20:00 12.717
21-Nov-19 07:31:00 12.511
14-Nov-19 21:19:00 14.144
22-Nov-19 04:53:00 13.541
21-Nov-19 07:35:00 12.557
27-Nov-19 04:21:00 11.184
01-Nov-19 05:40:00 15.014
11-Nov-19 00:27:00 14.869
10-Nov-19 22:38:00 15.037
01-Dec-19 17:15:00 13.61
02-Dec-19 05:19:00 8.46
03-Dec-19 01:37:00 11.733
02-Dec-19 18:17:00 13.778
05-Dec-19 00:55:00 9.978
02-Dec-19 22:17:00 12.191
01-Dec-19 00:14:00 15.411
08-Dec-19 08:04:00 10.802
02-Dec-19 01:40:00 9.421
02-Dec-19 09:13:00 11.802
08-Dec-19 06:44:00 11.512
08-Dec-19 08:04:00 10.802
02-Dec-19 00:14:00 10.947
09-Dec-19 01:48:00 10.672
02-Dec-19 00:45:00 9.635
02-Dec-19 00:27:00 10.299
02-Dec-19 04:16:00 9.749
09-Dec-19 00:49:00 10.848
08-Dec-19 06:34:00 11.382
02-Dec-19 09:56:00 13.488
02-Dec-19 09:32:00 12.733
02-Dec-19 16:40:00 15.823
05-Dec-19 01:11:00 12.809
03-Dec-19 03:49:00 13.572
02-Dec-19 01:33:00 9.826
26-Dec-19 06:43:00 6.88
27-Dec-19 06:25:00 6.27
27-Dec-19 04:07:00 6.674
27-Dec-19 06:06:00 6.621
30-Dec-19 07:47:00 5.4
31-Dec-19 23:44:00 5.606
Star Strider
2023-2-2
To calculate the ranges, it would likely be better to calculate something other than the the minutely ranges and then the maximum and minimum ranges from that. A range will not work for the ‘minutely’ values, since there is only one temperature recorded each minute.
There is simply no possible way to calculate a range from single values, since there is no variation in them.
A fix for that is to aggregate over small intervals, and for this I chose:
'Timestep',minutes(2)
in ‘MinutelyMin’ and ‘MinutelyMax’ and from those I calculated the ranges. Other intervals are of course possible. The ‘Monthly’ range values were calcualted from the ‘Minutely’ values, since it is higholy unlikely that a monthly range is going to be in any way equivalent to a range over 2 minutes (or whatever aggregation interval is used). The reason of course is that the ranges are unique values, and do not exist in the original data. They first must be calculated.
With those changes, it works for the ranges —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
MinutelyMin = retime(TT1, 'regular', 'min', 'Timestep',minutes(2))
MinutelyMin = 262800×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:02:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:18:00 10.139
01-Jan-19 00:20:00 10.138
01-Jan-19 00:22:00 10.138
01-Jan-19 00:24:00 10.138
01-Jan-19 00:26:00 10.137
01-Jan-19 00:28:00 10.137
01-Jan-19 00:30:00 10.138
MinutelyMax = retime(TT1, 'regular', 'max', 'Timestep',minutes(2))
MinutelyMax = 262800×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:02:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:18:00 10.139
01-Jan-19 00:20:00 10.139
01-Jan-19 00:22:00 10.138
01-Jan-19 00:24:00 10.138
01-Jan-19 00:26:00 10.137
01-Jan-19 00:28:00 10.137
01-Jan-19 00:30:00 10.138
MinutelyRange = table(MinutelyMax.Temperature - MinutelyMin.Temperature, 'VariableNames',{'Temperature'});
MinutelyRange = addvars(MinutelyRange, MinutelyMin.date_time, 'Before','Temperature');
MinutelyRange.Properties.VariableNames = {'date_time','Temperature'};
MinutelyRange = table2timetable(MinutelyRange)
MinutelyRange = 262800×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0
01-Jan-19 00:02:00 0
01-Jan-19 00:04:00 0.00018
01-Jan-19 00:06:00 0.00018
01-Jan-19 00:08:00 0.00036
01-Jan-19 00:10:00 0.00018
01-Jan-19 00:12:00 0.00037
01-Jan-19 00:14:00 0.00018
01-Jan-19 00:16:00 0.00018
01-Jan-19 00:18:00 0.00019
01-Jan-19 00:20:00 0.00055
01-Jan-19 00:22:00 0.00018
01-Jan-19 00:24:00 0.00037
01-Jan-19 00:26:00 0.00036
01-Jan-19 00:28:00 0
01-Jan-19 00:30:00 0.00018
MonthlyRangeMin = retime(MinutelyRange, 'monthly', 'min')
MonthlyRangeMin = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0
01-Feb-19 00:00:00 0
01-Mar-19 00:00:00 0
01-Apr-19 00:00:00 0
01-May-19 00:00:00 0
01-Jun-19 00:00:00 0
01-Jul-19 00:00:00 0
01-Aug-19 00:00:00 0
01-Sep-19 00:00:00 0
01-Oct-19 00:00:00 0
01-Nov-19 00:00:00 0
01-Dec-19 00:00:00 0
MonthlyRangeMax = retime(MinutelyRange, 'monthly', 'max')
MonthlyRangeMax = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0.00549
01-Feb-19 00:00:00 0.01117
01-Mar-19 00:00:00 0.03645
01-Apr-19 00:00:00 0.08332
01-May-19 00:00:00 0.00787
01-Jun-19 00:00:00 0.06977
01-Jul-19 00:00:00 0.00531
01-Aug-19 00:00:00 0.00385
01-Sep-19 00:00:00 0.04743
01-Oct-19 00:00:00 0.00933
01-Nov-19 00:00:00 0.01026
01-Dec-19 00:00:00 0.00751
MonthlyRange = table(MonthlyRangeMax.Temperature - MonthlyRangeMin.Temperature, 'VariableNames',{'Temperature'});
MonthlyRange = addvars(MonthlyRange, MonthlyRangeMin.date_time, 'Before','Temperature');
MonthlyRange.Properties.VariableNames = {'date_time','Temperature'};
MonthlyRange = table2timetable(MonthlyRange);
MonthDayRange = MonthlyToMinutely(MonthlyRange,MinutelyRange)
MonthDayRange = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 04:20:00 0.00549
03-Feb-19 21:26:00 0.01117
22-Mar-19 09:36:00 0.03645
19-Apr-19 16:10:00 0.08332
25-May-19 06:56:00 0.00787
29-Jun-19 18:16:00 0.06977
18-Jul-19 13:52:00 0.00531
22-Aug-19 08:36:00 0.00385
14-Sep-19 11:48:00 0.04743
04-Oct-19 04:10:00 0.00933
07-Nov-19 18:44:00 0.01026
04-Dec-19 03:38:00 0.00751
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
This is the only possible way to calculate the ranges in order to conform to the other calculations.
.
Ancalagon8
2023-2-3
编辑:Ancalagon8
2023-2-3
Thank you for your answer @Star Strider, everything works fine, but still MonthlyRangeMin and MonthlyRangeMax date_time are 00:00:00 everywhere. I mean there is no time determination when the max or min range per month occured.
Star Strider
2023-2-3
My pleasure!
The ‘MonthlyRangeMin’ and ‘MonthlyRangeMax’ default to midnight on the first of the month for the monthly values. That is simply how retime works. Similarly, the ‘Minutely’ values would default to zero seconds of the appropriate minute if seconds were reported.
The ‘MonthlyDayRange’ (or whatever you choose to call it, if different from my code) reports the dates and times —
MonthDayRange = 12×2 table
date_time Temperature
__________________ ___________
25-Jan-19 04:20:00 0.00549
03-Feb-19 21:26:00 0.01117
22-Mar-19 09:36:00 0.03645
19-Apr-19 16:10:00 0.08332
25-May-19 06:56:00 0.00787
29-Jun-19 18:16:00 0.06977
18-Jul-19 13:52:00 0.00531
22-Aug-19 08:36:00 0.00385
14-Sep-19 11:48:00 0.04743
04-Oct-19 04:10:00 0.00933
My ‘MonthlyToMinutely’ function turns out to be reasonably robust, so experiment with it with different arguments to get the result you want.
We used it earlier to get the hourly maxima and minima. The range values must be calculated from the 2-minute ranges (or whatever interval you choose) because the ranges are not an inherent part of the original data, that report single values only. The ranges must be calculated from them, although the accumulation interval for the ranges can be anything you want. I chose 2 minutes here because it is the shortest interval these data permit.
.
Ancalagon8
2023-2-3
I understand but the contents of MonthDayRange as a result do not look correct to me, as I expected the range between the max and min value to have larger values than 0.00549, 0.01117 etc. My goal is to calculate the 365 Daily ranges and then for every month to calculate the max, min and mean. Forgive me if it is not clear or I keep repeating myself, but I am trying to explain better. Thank you for your help so far!
Star Strider
2023-2-3
To the best of my knowledge, the aggregation time of the values for the range was not previously defined. For the daily range, this is straightforward.
Try this —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
TT1 = LD.TT1
TT1 = 513077×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.14
01-Jan-19 00:03:00 10.14
01-Jan-19 00:04:00 10.14
01-Jan-19 00:05:00 10.14
01-Jan-19 00:06:00 10.139
01-Jan-19 00:07:00 10.139
01-Jan-19 00:08:00 10.139
01-Jan-19 00:09:00 10.139
01-Jan-19 00:10:00 10.139
01-Jan-19 00:11:00 10.139
01-Jan-19 00:12:00 10.139
01-Jan-19 00:13:00 10.139
01-Jan-19 00:14:00 10.139
01-Jan-19 00:15:00 10.139
01-Jan-19 00:16:00 10.139
01-Jan-19 00:17:00 10.139
% MinutelyMin = retime(TT1, 'regular', 'min', 'Timestep',minutes(2))
%
% MinutelyMax = retime(TT1, 'regular', 'max', 'Timestep',minutes(2))
%
% MinutelyRange = table(MinutelyMax.Temperature - MinutelyMin.Temperature, 'VariableNames',{'Temperature'});
% MinutelyRange = addvars(MinutelyRange, MinutelyMin.date_time, 'Before','Temperature');
% MinutelyRange.Properties.VariableNames = {'date_time','Temperature'};
% MinutelyRange = table2timetable(MinutelyRange)
DailyMin = retime(TT1, 'daily', 'min')
DailyMin = 365×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.134
02-Jan-19 00:00:00 10.083
03-Jan-19 00:00:00 10.05
04-Jan-19 00:00:00 10.137
05-Jan-19 00:00:00 10.138
06-Jan-19 00:00:00 10.122
07-Jan-19 00:00:00 10.161
08-Jan-19 00:00:00 10.183
09-Jan-19 00:00:00 10.006
10-Jan-19 00:00:00 10.01
11-Jan-19 00:00:00 10.109
12-Jan-19 00:00:00 10.077
13-Jan-19 00:00:00 10.084
14-Jan-19 00:00:00 10.002
15-Jan-19 00:00:00 10.006
16-Jan-19 00:00:00 10.19
DailyMax = retime(TT1, 'daily', 'max')
DailyMax = 365×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 10.18
02-Jan-19 00:00:00 10.174
03-Jan-19 00:00:00 10.139
04-Jan-19 00:00:00 10.182
05-Jan-19 00:00:00 10.166
06-Jan-19 00:00:00 10.162
07-Jan-19 00:00:00 10.221
08-Jan-19 00:00:00 10.233
09-Jan-19 00:00:00 10.184
10-Jan-19 00:00:00 10.109
11-Jan-19 00:00:00 10.152
12-Jan-19 00:00:00 10.119
13-Jan-19 00:00:00 10.12
14-Jan-19 00:00:00 10.084
15-Jan-19 00:00:00 10.189
16-Jan-19 00:00:00 10.241
DailyRange = table2timetable(table(DailyMin.date_time, DailyMax.Temperature - DailyMin.Temperature, 'VariableNames',{'date_time','Temperature'}))
DailyRange = 365×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0.04597
02-Jan-19 00:00:00 0.09137
03-Jan-19 00:00:00 0.089
04-Jan-19 00:00:00 0.04504
05-Jan-19 00:00:00 0.02765
06-Jan-19 00:00:00 0.04029
07-Jan-19 00:00:00 0.06061
08-Jan-19 00:00:00 0.05054
09-Jan-19 00:00:00 0.17799
10-Jan-19 00:00:00 0.09925
11-Jan-19 00:00:00 0.04321
12-Jan-19 00:00:00 0.04194
13-Jan-19 00:00:00 0.03571
14-Jan-19 00:00:00 0.08241
15-Jan-19 00:00:00 0.18275
16-Jan-19 00:00:00 0.051641
MonthlyRangeMin = retime(DailyRange, 'monthly', 'min')
MonthlyRangeMin = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0.01758
01-Feb-19 00:00:00 0.01977
01-Mar-19 00:00:00 0.02014
01-Apr-19 00:00:00 0.01721
01-May-19 00:00:00 0.01703
01-Jun-19 00:00:00 0.01629
01-Jul-19 00:00:00 0.01703
01-Aug-19 00:00:00 0.01135
01-Sep-19 00:00:00 0.01373
01-Oct-19 00:00:00 0.01593
01-Nov-19 00:00:00 0.00751
01-Dec-19 00:00:00 0.01722
MonthlyRangeMax = retime(DailyRange, 'monthly', 'max')
MonthlyRangeMax = 12×1 timetable
date_time Temperature
__________________ ___________
01-Jan-19 00:00:00 0.18275
01-Feb-19 00:00:00 0.11226
01-Mar-19 00:00:00 0.09815
01-Apr-19 00:00:00 0.09944
01-May-19 00:00:00 0.10896
01-Jun-19 00:00:00 0.09028
01-Jul-19 00:00:00 0.08588
01-Aug-19 00:00:00 0.052
01-Sep-19 00:00:00 0.06117
01-Oct-19 00:00:00 0.1335
01-Nov-19 00:00:00 0.11464
01-Dec-19 00:00:00 0.17781
% MonthlyRange = table(MonthlyRangeMax.Temperature - MonthlyRangeMin.Temperature, 'VariableNames',{'Temperature'});
% MonthlyRange = addvars(MonthlyRange, MonthlyRangeMin.date_time, 'Before','Temperature');
% MonthlyRange.Properties.VariableNames = {'date_time','Temperature'};
% MonthlyRange = table2timetable(MonthlyRange)
MonthDayRangeMax = MonthlyToMinutely(MonthlyRangeMax,DailyRange) % Output: Day In Month With Maximum Range
MonthDayRangeMax = 12×2 table
date_time Temperature
__________________ ___________
15-Jan-19 00:00:00 0.18275
11-Feb-19 00:00:00 0.11226
11-Mar-19 00:00:00 0.09815
19-Apr-19 00:00:00 0.09944
04-May-19 00:00:00 0.10896
29-Jun-19 00:00:00 0.09028
10-Jul-19 00:00:00 0.08588
03-Aug-19 00:00:00 0.052
24-Sep-19 00:00:00 0.06117
08-Oct-19 00:00:00 0.1335
24-Nov-19 00:00:00 0.11464
14-Dec-19 00:00:00 0.17781
MonthDayRangeMin = MonthlyToMinutely(MonthlyRangeMin,DailyRange) % Output: Day In Month With Mionimum Range
MonthDayRangeMin = 12×2 table
date_time Temperature
__________________ ___________
21-Jan-19 00:00:00 0.01758
19-Feb-19 00:00:00 0.01977
07-Mar-19 00:00:00 0.02014
11-Apr-19 00:00:00 0.01721
01-May-19 00:00:00 0.01703
03-Jun-19 00:00:00 0.01629
21-Jul-19 00:00:00 0.01703
19-Aug-19 00:00:00 0.01135
08-Sep-19 00:00:00 0.01373
30-Oct-19 00:00:00 0.01593
25-Nov-19 00:00:00 0.00751
17-Dec-19 00:00:00 0.01722
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT) % FUNCTION: 'MonthlyToMinutely'
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
The times are all at midnight (00:00:00) because the ranges are now daily. The retime function defaults to midnight for daily aggregation.
And again, ‘MonthlyToMinuitely’ works here and gives the correct result!
.
Ancalagon8
2023-2-3
We totally agree, the only missing thing is here:
MonthDayRangeMax % Output: Day In Month With Maximum Range
MonthDayRangeMin % Output: Day In Month With Minimum Range
where as output I need not only the day but also the hour and minute.
All the other part of the code runs perfect!
Star Strider
2023-2-3
For a daily range (over 24 hours), the hour and minute are not defined. You can calculate a daily maximum and daily minimum and get those times (I believe we already did that a while ago), however they will by definition not be the same times, so there will not be any one specific time for the range.
Probably the best you can do is to store the times for the maximum and minimum for a specific day and the times they occurred, and save them each in single rows for each day (and the range, if desired, as a separate variable) in a separate table (not a timetable, since it requires a single time vector). We already did those calculations, and I have examples of how to create that sort of table in the code I already posted, although not in that exact format.
Because you wanted the hour and minute, I initially defined the range over consecutive 2-minute intervals.
Ancalagon8
2023-2-8
Problem solved with the temperature data. Using the same code in wind data, where I wanted to calculate the daily max value and the exact time the max value appeared per day, I retrieve a result where again I do not have a unique value per day. Am I missing something?
MinutelyMax = retime(T, 'minutely', 'max');
DailyMax = retime(T, 'daily', 'max');
MonthDayMax = MonthlyToMinutely(DailyMax,MinutelyMax)
or
[yhn,mhn,dhn] = ymd(MinutelyMax.date_time);
for k = 1:numel(DailyMax)
[ym,mm,dm] = ymd(DailyMax.date_time(k));
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyMax{:,1}(Lv) == DailyMax{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyMax(Mv(Dv(1)),:);
MonthDayMax(k,:) = timetable2table(HM);
end
ExactTime = MonthDayMax;
end
Star Strider
2023-2-8
I am slightly lost. I have no idea where we are with this data set.
My guess is that we’re doing this —
LD = load(websave('T','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289405/T.mat'));
TT1 = LD.T
TT1 = 513077×1 timetable
date_time Wind
__________________ ______
01-Jan-19 00:00:00 -5.624
01-Jan-19 00:03:00 -5.624
01-Jan-19 00:04:00 -5.818
01-Jan-19 00:05:00 -5.818
01-Jan-19 00:06:00 -6.012
01-Jan-19 00:07:00 -6.206
01-Jan-19 00:08:00 -6.788
01-Jan-19 00:09:00 -5.818
01-Jan-19 00:10:00 -5.43
01-Jan-19 00:11:00 -5.818
01-Jan-19 00:12:00 -6.012
01-Jan-19 00:13:00 -5.43
01-Jan-19 00:14:00 -6.012
01-Jan-19 00:15:00 -5.236
01-Jan-19 00:16:00 -5.818
01-Jan-19 00:17:00 -6.206
% return
% LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
% TT1 = LD.TT1
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
MonthDayMax = 12×2 table
date_time Wind
__________________ ______
28-Jan-19 12:48:00 783.79
13-Feb-19 13:23:00 894.92
29-Mar-19 12:24:00 1023.9
11-Apr-19 13:17:00 1214
30-May-19 13:29:00 1280.9
29-Jun-19 14:46:00 1123.2
14-Jul-19 13:46:00 1132.7
04-Aug-19 13:57:00 966.11
03-Sep-19 12:45:00 1053.8
02-Oct-19 13:54:00 946.32
08-Nov-19 11:50:00 777
13-Dec-19 12:18:00 738.4
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
MonthDayMin = 12×2 table
date_time Wind
__________________ _______
11-Jan-19 22:59:00 -15.904
23-Feb-19 22:45:00 -13.189
27-Mar-19 23:36:00 -12.413
23-Apr-19 19:17:00 -18.232
04-May-19 21:30:00 -18.813
29-Jun-19 01:59:00 -16.68
10-Jul-19 21:13:00 -18.426
29-Aug-19 04:04:00 -7.952
14-Sep-19 06:08:00 -9.697
06-Oct-19 00:50:00 -19.589
17-Nov-19 21:04:00 -17.65
26-Dec-19 17:52:00 -18.62
MinutelyMax = retime(TT1, 'minutely', 'max');
MonthlyMax = retime(TT1, 'monthly', 'max');
MonthDayMax = MonthlyToMinutely(MonthlyMax,MinutelyMax)
MonthDayMax = 12×2 table
date_time Wind
__________________ ______
28-Jan-19 12:48:00 783.79
13-Feb-19 13:23:00 894.92
29-Mar-19 12:24:00 1023.9
11-Apr-19 13:17:00 1214
30-May-19 13:29:00 1280.9
29-Jun-19 14:46:00 1123.2
14-Jul-19 13:46:00 1132.7
04-Aug-19 13:57:00 966.11
03-Sep-19 12:45:00 1053.8
02-Oct-19 13:54:00 946.32
08-Nov-19 11:50:00 777
13-Dec-19 12:18:00 738.4
MinutelyMin = retime(TT1, 'minutely', 'min');
MonthlyMin = retime(TT1, 'monthly', 'min');
MonthDayMin = MonthlyToMinutely(MonthlyMin,MinutelyMin)
MonthDayMin = 12×2 table
date_time Wind
__________________ _______
11-Jan-19 22:59:00 -15.904
23-Feb-19 22:45:00 -13.189
27-Mar-19 23:36:00 -12.413
23-Apr-19 19:17:00 -18.232
04-May-19 21:30:00 -18.813
29-Jun-19 01:59:00 -16.68
10-Jul-19 21:13:00 -18.426
29-Aug-19 04:04:00 -7.952
14-Sep-19 06:08:00 -9.697
06-Oct-19 00:50:00 -19.589
17-Nov-19 21:04:00 -17.65
26-Dec-19 17:52:00 -18.62
function ExactTime = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
I have no idea what the units are here, however it seems to work when I run it.
There may not be unique values in each daily data set for the maxima and minima. My code defaults to whatever retime does in those situations. In my ‘MonthlyToMinutely’ function, the ‘idx’ vector is a logical vector, and could return multiple results. I solve that problem by returning the first result (the ‘HM’ assignment in the loop) rather than all of them, because returning all of them would disrupt the table result. It allows for only one result per row. Returning all of them causes problems with the table format.
.
Ancalagon8
2023-2-8
Thanks for the detailed answer once more! What I mean here is that the needed output is a 365X2 table with one max value per day and the date/time/hour/minute that max value occured. The current output has not a unique daily max value.
Star Strider
2023-2-9
编辑:Star Strider
2023-2-9
It seems to me that would simply be this —
LD = load(websave('T','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289405/T.mat'));
TT1 = LD.T
TT1 = 513077×1 timetable
date_time Wind
__________________ ______
01-Jan-19 00:00:00 -5.624
01-Jan-19 00:03:00 -5.624
01-Jan-19 00:04:00 -5.818
01-Jan-19 00:05:00 -5.818
01-Jan-19 00:06:00 -6.012
01-Jan-19 00:07:00 -6.206
01-Jan-19 00:08:00 -6.788
01-Jan-19 00:09:00 -5.818
01-Jan-19 00:10:00 -5.43
01-Jan-19 00:11:00 -5.818
01-Jan-19 00:12:00 -6.012
01-Jan-19 00:13:00 -5.43
01-Jan-19 00:14:00 -6.012
01-Jan-19 00:15:00 -5.236
01-Jan-19 00:16:00 -5.818
01-Jan-19 00:17:00 -6.206
% return
% LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
% TT1 = LD.TT1
MinutelyMax = retime(TT1, 'minutely', 'max');
DailyMax = retime(TT1, 'daily', 'max');
DayMinuteMax = MonthlyToMinutely(DailyMax,MinutelyMax);
DayMinuteMax
DayMinuteMax = 365×2 table
date_time Wind
__________________ ______
01-Jan-19 12:48:00 549.1
02-Jan-19 10:56:00 495.95
03-Jan-19 15:36:00 284.34
04-Jan-19 13:11:00 667.41
05-Jan-19 12:43:00 504.87
06-Jan-19 14:11:00 534.16
07-Jan-19 12:42:00 675.17
01-Jan-19 12:52:00 542.11
05-Jan-19 09:19:00 103.96
10-Jan-19 12:26:00 702.32
01-Jan-19 12:47:00 548.9
01-Jan-19 11:00:00 350.87
13-Jan-19 12:27:00 543.28
06-Jan-19 10:59:00 417.79
15-Jan-19 12:46:00 537.46
15-Jan-19 13:21:00 529.51
MinutelyMin = retime(TT1, 'minutely', 'min');
DailyMin = retime(TT1, 'daily', 'min');
DayMinuteMin = MonthlyToMinutely(DailyMin,MinutelyMin);
DayMinuteMin
DayMinuteMin = 365×2 table
date_time Wind
__________________ _______
01-Jan-19 22:10:00 -11.637
02-Jan-19 07:24:00 -12.413
01-Jan-19 18:53:00 -10.667
04-Jan-19 01:28:00 -12.801
05-Jan-19 22:35:00 -12.995
04-Jan-19 01:28:00 -12.801
07-Jan-19 02:06:00 -13.383
08-Jan-19 05:34:00 -14.74
02-Jan-19 00:36:00 -11.831
01-Jan-19 21:49:00 -10.861
11-Jan-19 22:59:00 -15.904
01-Jan-19 22:53:00 -11.443
04-Jan-19 01:28:00 -12.801
02-Jan-19 01:22:00 -12.219
02-Jan-19 00:36:00 -11.831
08-Jan-19 06:14:00 -13.577
% G = findgroups(day(AllMin.date_time),month(AllMin.date_time),hour(AllMin.date_time),minute(AllMin.date_time),AllMin{:,1});
% MinutelyMax = retime(TT1, 'minutely', 'max');
% DailyMax = retime(TT1, 'daily', 'max');
% DayMinujteMax = MonthlyToMinutely(DailyMax,MinutelyMax)
%
% MinutelyMin = retime(TT1, 'minutely', 'min');
% DailyMin = retime(TT1, 'daily', 'min');
% DayMinuteMin = MonthlyToMinutely(DailyMin,MinutelyMin)
function [ExactTime,AllMatches] = MonthlyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
Lv = ismember([yhn,mhn], [ym,mm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
end
As I mentioned, my ‘MonthlyToMinutely’ function returns the first instance of the match that it finds, if there are more than one. This preserves the integrity of the ‘ExactTime’ output table.
I changed it to return multiple times for the same values as the second output, if the variable values are not unique. That works for the minimum values, however it’s returning anomalous results for the maximum values. I’ll work on it offlline and include it when I understand what the problem is.
EDIT — Forgot to run the code before I submitted it.
.
Ancalagon8
2023-2-9
Ok @Star Strider, the code runs fine, the only thing is how to eliminate the appearance of multiple max values per day. Thanks you in advance!
Star Strider
2023-2-9
Only one max value per day appears in the ‘ExactTime’ result, although there can be more than one detected. My ‘MonthlyToMinutely’ function returns the first one it finds.
The problem was the resolution in the two arrays. For the time being, I’m using two different functions. I’ll work on the function to add an option (extra arguments) to make it more flexible. It will likely take some time to get that working properly. In the interim, the differences are explained by the code in the different functions, specifically in the ‘Lv’ and ‘idx’ assignments.
Try this —
LD = load(websave('T','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289405/T.mat'));
TT1 = LD.T
TT1 = 513077×1 timetable
date_time Wind
__________________ ______
01-Jan-19 00:00:00 -5.624
01-Jan-19 00:03:00 -5.624
01-Jan-19 00:04:00 -5.818
01-Jan-19 00:05:00 -5.818
01-Jan-19 00:06:00 -6.012
01-Jan-19 00:07:00 -6.206
01-Jan-19 00:08:00 -6.788
01-Jan-19 00:09:00 -5.818
01-Jan-19 00:10:00 -5.43
01-Jan-19 00:11:00 -5.818
01-Jan-19 00:12:00 -6.012
01-Jan-19 00:13:00 -5.43
01-Jan-19 00:14:00 -6.012
01-Jan-19 00:15:00 -5.236
01-Jan-19 00:16:00 -5.818
01-Jan-19 00:17:00 -6.206
MinutelyMax = retime(TT1, 'minutely', 'max');
DailyMax = retime(TT1, 'daily', 'max');
[DayMinuteMax,AllMax] = DailyToMinutely(DailyMax,MinutelyMax);
DayMinuteMax
DayMinuteMax = 365×2 table
date_time Wind
__________________ ______
01-Jan-19 12:48:00 549.1
02-Jan-19 10:56:00 495.95
03-Jan-19 15:36:00 284.34
04-Jan-19 13:11:00 667.41
05-Jan-19 12:43:00 504.87
06-Jan-19 14:11:00 534.16
07-Jan-19 12:42:00 675.17
08-Jan-19 12:42:00 542.11
09-Jan-19 14:14:00 103.96
10-Jan-19 12:26:00 702.32
11-Jan-19 12:29:00 548.9
12-Jan-19 13:48:00 350.87
13-Jan-19 12:27:00 543.28
14-Jan-19 11:07:00 417.79
15-Jan-19 12:46:00 537.46
16-Jan-19 13:00:00 529.51
AllMax = cat(1,AllMax{:})
AllMax = 397×1 timetable
date_time Wind
__________________ ______
01-Jan-19 12:48:00 549.1
02-Jan-19 10:56:00 495.95
03-Jan-19 15:36:00 284.34
04-Jan-19 13:11:00 667.41
05-Jan-19 12:43:00 504.87
06-Jan-19 14:11:00 534.16
07-Jan-19 12:42:00 675.17
08-Jan-19 12:42:00 542.11
09-Jan-19 14:14:00 103.96
10-Jan-19 12:26:00 702.32
11-Jan-19 12:29:00 548.9
11-Jan-19 12:30:00 548.9
12-Jan-19 13:48:00 350.87
13-Jan-19 12:27:00 543.28
14-Jan-19 11:07:00 417.79
15-Jan-19 12:46:00 537.46
MinutelyMin = retime(TT1, 'minutely', 'min');
DailyMin = retime(TT1, 'daily', 'min');
[DayMinuteMin,AllMin] = DailyToMinutely(DailyMin,MinutelyMin);
DayMinuteMin
DayMinuteMin = 365×2 table
date_time Wind
__________________ _______
01-Jan-19 22:10:00 -11.637
02-Jan-19 07:24:00 -12.413
03-Jan-19 23:47:00 -10.667
04-Jan-19 01:28:00 -12.801
05-Jan-19 22:35:00 -12.995
06-Jan-19 00:03:00 -12.801
07-Jan-19 02:06:00 -13.383
08-Jan-19 05:34:00 -14.74
09-Jan-19 03:50:00 -11.831
10-Jan-19 17:52:00 -10.861
11-Jan-19 22:59:00 -15.904
12-Jan-19 22:23:00 -11.443
13-Jan-19 04:33:00 -12.801
14-Jan-19 02:24:00 -12.219
15-Jan-19 23:30:00 -11.831
16-Jan-19 06:17:00 -13.577
AllMin = cat(1,AllMin{:})
AllMin = 570×1 timetable
date_time Wind
__________________ _______
01-Jan-19 22:10:00 -11.637
01-Jan-19 22:11:00 -11.637
02-Jan-19 07:24:00 -12.413
03-Jan-19 23:47:00 -10.667
04-Jan-19 01:28:00 -12.801
05-Jan-19 22:35:00 -12.995
05-Jan-19 23:20:00 -12.995
06-Jan-19 00:03:00 -12.801
06-Jan-19 20:50:00 -12.801
06-Jan-19 20:56:00 -12.801
06-Jan-19 23:02:00 -12.801
07-Jan-19 02:06:00 -13.383
07-Jan-19 02:29:00 -13.383
08-Jan-19 05:34:00 -14.74
09-Jan-19 03:50:00 -11.831
09-Jan-19 03:51:00 -11.831
function [ExactTime,AllMatches] = DailyToMinutely(MonthlyTT,MinutelyTT)
MinutelyVN = MinutelyTT.Properties.VariableNames;
MonthlyVN = MonthlyTT.Properties.VariableNames;
if ~strcmp(MinutelyVN{:}, MonthlyVN{:})
ExactTime = timetable();
fprintf('Variable names %s and %s in the argument timetables are not the same.',MonthlyVN{:},MinutelyVN{:})
return
end
[yhn,mhn,dhn] = ymd(MinutelyTT.date_time); % Year, Month, Day Of Daily Maxima
for k = 1:numel(MonthlyTT)
[ym,mm,dm] = ymd(MonthlyTT.date_time(k)); % Year, Month, Day Of Monthily Maxima
% YrMoDa = [ym mm dm]
Lv = ismember([yhn,mhn,dhn], [ym,mm,dm], 'rows');
idx = MinutelyTT{:,1}(Lv) == MonthlyTT{:,1}(k);
% LvSize = nnz(Lv)
Mv = find(Lv);
Dv = find(idx);
HM = MinutelyTT(Mv(Dv(1)),:);
AllHM{k,:} = MinutelyTT(Mv(Dv),:);
MonthDayTT(k,:) = timetable2table(HM);
end
ExactTime = MonthDayTT;
AllMatches = AllHM;
end
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)