Sort a matrix with respect to a date.

1 visualización (últimos 30 días)
Marty Dutch
Marty Dutch el 17 de Sept. de 2015
Editada: Stephen23 el 17 de Sept. de 2015
Hi experts, is there a way to sort variables in a matrix based on date? For instance, here I want to first sort the variables based on column 3 (0s first then 1s) and then based on date (earliest dates first). See for an example below.
list =
{3436x1 cell} [3436x11 char] [3436x1 double]
'BG4704_01' 03-Jan-2015 0
'BG4705_01' 02-Jan-2015 1
'BG4706_01' 21-Dec-2014 0
'BG4707_01' 23-Oct-1913 1
'BG4708_01' 05-Jul-1913 1

Respuesta aceptada

Stephen23
Stephen23 el 17 de Sept. de 2015
Editada: Stephen23 el 17 de Sept. de 2015
Probably the easiest way is to simply convert those date strings to date numbers, which can then be sorted together with the binary values:
% original data in X
X{1} = {'BG4704_01';'BG4705_01';'BG4706_01';'BG4707_01';'BG4708_01'};
X{2} = ['03-Jan-2015';'02-Jan-2015';'21-Dec-2014';'23-Oct-1913';'05-Jul-1913'];
X{3} = [0;1;0;1;1];
% create numeric matrix Y and sort based on binary then dates:
Y(:,2) = datenum(X{2},'dd-mmm-yyyy');
Y(:,1) = X{3};
[~,idx] = sortrows(Y)
% rearrange data inside cell vector X
for k = 1:numel(X)
X{k} = X{k}(idx,:);
end
  1 comentario
Marty Dutch
Marty Dutch el 17 de Sept. de 2015
Thanks a lot, Stephen. This is really helpful!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by