Sort a matrix with respect to a date.

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 17 Sep 2015
Edited: Stephen23 on 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

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!