Retime Yearly Maximum - Finding Corresponding Dates from Timetable
조회 수: 10 (최근 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!