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

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 Comment

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?

Sign in to comment.

Answers (1)

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 Comments

For some reason when I try the first method I end up with a vector (nextidx) full of NaNs.
tt=flood; ts = MX;
nextidx = interp1(tt, 1:length(tt), ts, 'next');
I did attempt a different approach which I think worked:
tidechange = flood; % times when tide direction changes
time2tide = []
upcoming_tide_idx = []
for i=1:length(MX)
temp = MX(tidechange) - MX(i)
temp(temp<0) = nan, [val,idx] = nanmin(temp) %find the lowest (positive) difference in the temporary vector
upcoming_tide_idx(i) = tidechange(idx); % save the index for the nearest future tide change
time2tide(i) = val; % time difference between current timestamp and time of upcomign tide change
end
Hmmm... could you show min(flood), max(flood), min(MX), max(MX) ?
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

Sign in to comment.

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Asked:

on 14 Sep 2020

Commented:

on 15 Sep 2020

Community Treasure Hunt

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

Start Hunting!