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
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!