フィルターのクリア

interpolate the duplicate values

5 ビュー (過去 30 日間)
mahdi Babayi semiromi
mahdi Babayi semiromi 2023 年 3 月 17 日
コメント済み: Rik 2023 年 4 月 5 日
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
mahdi Babayi semiromi 2023 年 4 月 5 日
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 Center および File ExchangeCalendar についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by