Sort a matrix with respect to a date.

1 Ansicht (letzte 30 Tage)
Marty Dutch
Marty Dutch am 17 Sep. 2015
Bearbeitet: Stephen23 am 17 Sep. 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

Akzeptierte Antwort

Stephen23
Stephen23 am 17 Sep. 2015
Bearbeitet: Stephen23 am 17 Sep. 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 Kommentar
Marty Dutch
Marty Dutch am 17 Sep. 2015
Thanks a lot, Stephen. This is really helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by