interpolate the duplicate values

3 次查看(过去 30 天)
Hi
Imagine I have vector of datetime values in increaing (not strictly increasing) order, such as follows:
'14-11-2022 05:18:14.000'
'14-11-2022 05:18:14.000'
'14-11-2022 05:18:14.000'
'14-11-2022 05:18:15.000'
'14-11-2022 05:18:15.000'
'14-11-2022 05:18:16.000'
'14-11-2022 05:18:17.000'
I want to convert every slice of duplicate values into an evenly-spaced slice, so the above vector becomes:
'14-11-2022 05:18:14.000'
'14-11-2022 05:18:14.333'
'14-11-2022 05:18:14.666'
'14-11-2022 05:18:15.000'
'14-11-2022 05:18:15.500'
'14-11-2022 05:18:16.000'
'14-11-2022 05:18:17.000'
I know how to do it with loops, just wondering if there's a clever way to do it, probably using accumarray and histcounts.
thanks
  3 个评论
mahdi Babayi semiromi
Hi
Yes each run of duplicate values will end at the next second.
thanks
Rik
Rik 2023-4-5
Well, what did you try? Have you looked into run length encoding?

请先登录,再进行评论。

回答(1 个)

Gayatri Rathod
Gayatri Rathod 2023-3-30
Hi mahdi,
you can use accumarray and histcounts to achieve this in MATLAB. Here's one way to do it:
1.Convert your date values to datetime format and create input vector of it (e.g., using datetime).
% Example input vector of datetime values
inputVec = datetime(dates);
2. Calculate the differences between adjacent serial dates using the diff function:
diffs = diff( inputVec);
3. Use histcounts to find the indices of the duplicate values:
[~, edges, bin] = histcounts(diffs);
idx = find(bin > 1);
4. use accumarray to add the appropriate fractions of a second to each group of duplicate values
% Syntax
accumarray(ind,data,[],fun) % applies the function fun to each group in data specified by ind.
% Specify fun using the @symbol. for eg. @sum.
5.Convert the resulting values back to appropriate datetime format if needed.
The resulting vector should be the evenly spaced version of the input dates.
You can read more about the accumarray, histcounts, datetime, diff and find function from the following documentations: accumarray function, histcounts function, datetime function, diff function, find function.
Hope it helps!  
Regards,
Gayatri Rathod

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by