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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by