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 view (last 30 days)
Show older comments
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
Adam Danz
on 15 Sep 2020
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?
Answers (1)
Walter Roberson
on 15 Sep 2020
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
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!