Sort a matrix with respect to a date.

조회 수: 1 (최근 30일)
Marty Dutch
Marty Dutch 2015년 9월 17일
편집: Stephen23 2015년 9월 17일
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

채택된 답변

Stephen23
Stephen23 2015년 9월 17일
편집: Stephen23 2015년 9월 17일
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
Marty Dutch
Marty Dutch 2015년 9월 17일
Thanks a lot, Stephen. This is really helpful!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by