Retime Yearly Maximum - Finding Corresponding Dates from Timetable
13 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Star Strider
2023-4-23
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'
WxYr = retime(Wx, 'yearly','max') % Yearly Maxima
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
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).
.
0 个评论
更多回答(1 个)
Eric Sofen
2023-4-24
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!