Retime Yearly Maximum - Finding Corresponding Dates from Timetable

5 views (last 30 days)
I'm using the retime function to find yearly maximum values for a timetable (e.g., x=timetable(Datetime, Var); YearlyMax = retime(x,'yearly','max')).
I need an efficient means of finding the corresponding date/time from the timetable for each maximum value rather than the 01-Jan-YYYY resulting timetable. Any ideas on the best way of finding the dates for each yearly maximum value with the timetable?

Accepted Answer

Star Strider
Star Strider on 23 Apr 2023
Try something like this —
Date = datetime(2020,01,01)+caldays(0:3.9*365.25).';
Temp = 80*rand(size(Date))-40;
Humd = randi([1 100],size(Date));
Wx = timetable(Date,Temp,Humd) % Original 'timetable'
Wx = 1425×2 timetable
Date Temp Humd ___________ _______ ____ 01-Jan-2020 22.507 100 02-Jan-2020 -5.5977 28 03-Jan-2020 29.767 24 04-Jan-2020 -30.072 81 05-Jan-2020 22.233 36 06-Jan-2020 2.1534 53 07-Jan-2020 -6.4102 32 08-Jan-2020 -20.92 17 09-Jan-2020 -11.942 45 10-Jan-2020 20.802 51 11-Jan-2020 31.433 81 12-Jan-2020 -16.857 81 13-Jan-2020 -12.293 100 14-Jan-2020 -8.2595 32 15-Jan-2020 -27.176 93 16-Jan-2020 12.763 34
WxYr = retime(Wx, 'yearly','max') % Yearly Maxima
WxYr = 4×2 timetable
Date Temp Humd ___________ ______ ____ 01-Jan-2020 39.961 100 01-Jan-2021 39.978 100 01-Jan-2022 39.891 100 01-Jan-2023 39.987 100
Yrs = year(WxYr.Date);
for k = 1:numel(Yrs)
Lv1 = year(Wx.Date) == Yrs(k); % Logical Vector Selecting Days For Selected Year
WxCurYr = Wx(Lv1,:); % Current Year 'timetable'
Lv2 = ismember(WxCurYr.Temp, WxYr(k,:).Temp); % Select Variable To Compare (Here: Temperature) & Return Logical Vector Of Matches
ResultTT{k,:} = WxCurYr(Lv2,:); % Save 'timetable' Arrays
Result(k,:) = timetable2table(WxCurYr(Lv2,:)); % Save Rows In The 'Result' Table
end
Result % Table Of Days Matching Yearly Maximum Temperature
Result = 4×3 table
Date Temp Humd ___________ ______ ____ 15-Nov-2020 39.961 65 12-Oct-2021 39.978 85 08-Jan-2022 39.891 85 06-May-2023 39.987 34
If there is more than one matching value, this becomes slightly more complicated. In that instance, it will be necessary to either return all of the matching days (this usually requires that ‘result’ becomes a cell array, causing further complications later in the code), or use find with ‘Lv2’ and use an element of that vector to return only one of them (the approach I have characteristically taken in these situations, simply because it’s easier).
.

More Answers (1)

Eric Sofen
Eric Sofen on 24 Apr 2023
This example gives another way to get the time when the max occurred. See the findMax function and how it's used in rowfun. The difference is you're grouping by year, rather than by site.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!