Borrar filtros
Borrar filtros

interpolate the duplicate values

5 visualizaciones (últimos 30 días)
mahdi Babayi semiromi
mahdi Babayi semiromi el 17 de Mzo. de 2023
Comentada: Rik el 5 de Abr. de 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 comentarios
mahdi Babayi semiromi
mahdi Babayi semiromi el 5 de Abr. de 2023
Hi
Yes each run of duplicate values will end at the next second.
thanks
Rik
Rik el 5 de Abr. de 2023
Well, what did you try? Have you looked into run length encoding?

Iniciar sesión para comentar.

Respuestas (1)

Gayatri Rathod
Gayatri Rathod el 30 de Mzo. de 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

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by