Finding unique dates in vector

Hi all.
I have a vector of dates where each date is repeated many times. How would i create a vector with every date but no repetitions efficiently?
Thx.

1 comentario

Jan
Jan el 25 de Mayo de 2011
What exactly means "vector of dates"? Please post the type of the input exactly. I assume that UNIQUE will help you directly.

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 25 de Mayo de 2011

0 votos

Date = ['02-Aug-2010 19:28:01'
'13-Nov-2010 09:10:20'
'10-Nov-2010 14:44:14'
'02-Aug-2010 19:28:01'
'08-Oct-2010 08:58:46'
'30-Jan-2010 08:24:16'
'10-Nov-2010 14:44:14'
'13-Nov-2010 09:10:20'
'08-Oct-2010 08:58:46']
[a n] = unique(datenum(Date));
D1 = sortrows([a n],2);
Dout = datestr(D1(:,1));
Hi Jan! agree with you. In our case, it is possible and so:
[a n] = unique(Date,'rows')
[~,idx] = sort(n);
Dout = a(idx,:);

4 comentarios

Jan
Jan el 25 de Mayo de 2011
What about "unique(cellstr(Date))"?
Mads
Mads el 25 de Mayo de 2011
Thanks a lot.
Andrei Bobrov
Andrei Bobrov el 25 de Mayo de 2011
Hi Jan! agree with you.
Matt Tearle
Matt Tearle el 25 de Mayo de 2011
...assuming that the dates are all in the exact same format. Andrei's solution (going via datenum) avoids the problem of two different formats for the same date. (Unlikely, but it could happen.)

Iniciar sesión para comentar.

Más respuestas (1)

Daniel Svedbrand
Daniel Svedbrand el 12 de Mzo. de 2025
Editada: Daniel Svedbrand el 12 de Mzo. de 2025

0 votos

unique_dates = unique(dateshift(Date,'start','day'));
if Date are datetimes, otherwise
unique_dates = unique(dateshift(datetime(Date),'start','day'));

Categorías

Etiquetas

Preguntada:

el 25 de Mayo de 2011

Editada:

el 12 de Mzo. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by