Clear Filters
Clear Filters

interpolate the duplicate values

5 views (last 30 days)
mahdi Babayi semiromi
mahdi Babayi semiromi on 17 Mar 2023
Commented: Rik on 5 Apr 2023
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 Comments
mahdi Babayi semiromi
mahdi Babayi semiromi on 5 Apr 2023
Hi
Yes each run of duplicate values will end at the next second.
thanks
Rik
Rik on 5 Apr 2023
Well, what did you try? Have you looked into run length encoding?

Sign in to comment.

Answers (1)

Gayatri Rathod
Gayatri Rathod on 30 Mar 2023
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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!