How do I determine the closest upcoming index (given a vector of the indices of interest) to each value in a vector of timestamps?

1 次查看(过去 30 天)
I have a record of timestamps at 5 minute intervals. I want to calculate the time (ideally in hours) until the next tide change given the indices of the tide changes. I think the first step is to determine the closest upcoming tide change index to each timestamp in the vector (I need to identify the NEXT tide change even if the previous tide change is closer).
I have attached the data here (data.m) where MX (4888x1 double) is my timestamp vector (in matlab time) and flood (32x1 double) identifies the indices of the flood tides starting.
I cannot for the life of me figure out how to translate this problem into Matlab and I'm not sure what to search in the Matlab help section to figure it out either.
Thank you in advance for any help!
  1 个评论
Adam Danz
Adam Danz 2020-9-15
If "MX" are your timestamps and "flood" are the indices of the time stamps that indicate tide-change, then the time of the tide changes are just MX(flood).
Is your goal to compute the time until the next tide change for each of the 4000+ timestamps?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-9-15
tt = vector_of_tide_times; %must be in increasing order
ts = vector_of_timestamps; %does not need to be in order
nextidx = interp1(tt, 1:length(tt), ts, 'next');
For each entry in ts, the value of nextidx would be the index into tt at which tt(nextidx(K)) is the next tide time after ts(K)
This is not the only possibility. For example, there are approaches involving
bin = discretize(-ts, fliplr(-tt));
nextidx = length(tt) - bin + 1;
The negatives there are because discretize takes the previous edge not the next edge, and you can map the "next edge" problem to that by taking the negatives of both sides so that the next edge is more negative and so is "previous" in the negative space.
  4 个评论
Heidi Hirsh
Heidi Hirsh 2020-9-15
min(flood) = 6
max(flood) = 4770
min(MX) = 7.3690e+05
max(MX) = 7.3692e+05
min(datetime(datevec(MX))) = 23-Jul-2017 12:00:00
max(datetime(datevec(MX))) = 09-Aug-2017 11:15:00

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by