Retime Yearly Maximum - Finding Corresponding Dates from Timetable
12 ビュー (過去 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!