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
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
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!