Find date time value that corresponds to maximum daily value

I have a dataset from a tide gauge that measures tide elevation every 6 minutes for 3 and a half years which adds up to over 200,000 data values. I was able to extract the maximum daily values, but I also want to know the corresponding date time values. So I have a vector of date time values, a vector of tide elevation values of the same length, and a subset of maximum daily tide values, and I want the corresponding date time values to that maximum daily tide subset. Can anyone help me? I can provide my dataset if that helps.

답변 (2개)

Peter Perkins
Peter Perkins 2019년 1월 23일
편집: Peter Perkins 2019년 1월 31일
I'm not sure retime will do all of what's asked for: it only works on one variable at a time. Getting the max tide for each day is easy, getting the specific time during each day when that happened is trickier. You can do it with retime, but I think it would take a few tricks, unless I'm not seeing something. Here's a version that uses a grouped rowfun. It requires one to convert the timetable (temporarily) to a table, because the row times of a timetable cannot curently be specified as an input to the function being applied.
>> X = rand(10,1);
>> tt = timetable(X,'RowTimes',datetime(2019,1,23,4:8:76,0,0))
tt =
10×1 timetable
Time X
____________________ _______
23-Jan-2019 04:00:00 0.95974
23-Jan-2019 12:00:00 0.34039
23-Jan-2019 20:00:00 0.58527
24-Jan-2019 04:00:00 0.22381
24-Jan-2019 12:00:00 0.75127
24-Jan-2019 20:00:00 0.2551
25-Jan-2019 04:00:00 0.50596
25-Jan-2019 12:00:00 0.69908
25-Jan-2019 20:00:00 0.8909
26-Jan-2019 04:00:00 0.95929
>> t = timetable2table(tt);
>> t.Day = dateshift(t.Time,'start','day');
t =
10×3 table
Time X Day
____________________ _______ ____________________
23-Jan-2019 04:00:00 0.95974 23-Jan-2019 00:00:00
23-Jan-2019 12:00:00 0.34039 23-Jan-2019 00:00:00
23-Jan-2019 20:00:00 0.58527 23-Jan-2019 00:00:00
24-Jan-2019 04:00:00 0.22381 24-Jan-2019 00:00:00
24-Jan-2019 12:00:00 0.75127 24-Jan-2019 00:00:00
24-Jan-2019 20:00:00 0.2551 24-Jan-2019 00:00:00
25-Jan-2019 04:00:00 0.50596 25-Jan-2019 00:00:00
25-Jan-2019 12:00:00 0.69908 25-Jan-2019 00:00:00
25-Jan-2019 20:00:00 0.8909 25-Jan-2019 00:00:00
26-Jan-2019 04:00:00 0.95929 26-Jan-2019 00:00:00
>>> rowfun(@myFun,t,'GroupingVariable','Day','OutputVariableNames',{'MaxTime' 'MaxX'})
ans =
4×4 table
Day GroupCount MaxTime MaxX
____________________ __________ ____________________ _______
23-Jan-2019 00:00:00 3 23-Jan-2019 04:00:00 0.95974
24-Jan-2019 00:00:00 3 24-Jan-2019 12:00:00 0.75127
25-Jan-2019 00:00:00 3 25-Jan-2019 20:00:00 0.8909
26-Jan-2019 00:00:00 1 26-Jan-2019 04:00:00 0.95929
And then convert that back to a timetable. I think you can also do the above with splitapply.
myFun looks like this:
function [tmax,xmax] = myFun(t,x)
[xmax,imax] = max(x,[],1);
tmax = t(imax);

댓글 수: 3

My original post left out two key parts of the code, t.Day = ..., and the definition of myFun. I've added them.
I am trying to do the same thing, but on many stations at once. Is that possible? Tried to do it on one station, but it gives me an error saying that there are too many input arguments.
Add a grouping variable for Station to what I have shown. "By station and day" is no different than "by day", just one more grouping variable.

댓글을 달려면 로그인하십시오.

Star Strider
Star Strider 2019년 1월 11일

0 개 추천

It will likely be best to create a timetable if your data, then use the retime function to calculate the maximum daily value. You might find the documentation section on Synchronize and Aggregate Data Values helpful to determine the time the maximum occurred.

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

질문:

2019년 1월 11일

댓글:

2021년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by