Calculate the maximum and minimum values and what day these values occured

13 Ansichten (letzte 30 Tage)
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 Kommentar
othman warde
othman warde am 14 Feb. 2023
Bearbeitet: Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 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.
  44 Kommentare
Ancalagon8
Ancalagon8 am 9 Feb. 2023
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
Star Strider am 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
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by