Calculate the maximum and minimum values

11 views (last 30 days)
Hello I have a big timetable I want to calculate the maximum and minimum values
Any ideas how to proceed?
  1 Comment
othman warde
othman warde on 14 Feb 2023
Edited: Walter Roberson on 14 Feb 2023
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.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Jan 2023
I thought we covered a version of that in my Comment to your other thread.
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.
  24 Comments
Star Strider
Star Strider on 9 Feb 2023
Edited: Star Strider on 9 Feb 2023
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.
.
Star Strider
Star Strider on 9 Feb 2023
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
.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Preprocessing 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!