sorting columns with empty cells

조회 수: 5 (최근 30일)
antonet
antonet 2012년 5월 27일
Dear all,'
I have the following column and as you can see the 1rst, 40th, 79th asnd so forth are mising
''
'23-11-2008'
'21-12-2008'
'18-01-2009'
'15-02-2009'
'15-03-2009'
'12-04-2009'
'10-05-2009'
'07-06-2009'
'05-07-2009'
'02-08-2009'
'30-08-2009'
'27-09-2009'
'25-10-2009'
'22-11-2009'
'20-12-2009'
'24-01-2010'
'21-02-2010'
'21-03-2010'
'18-04-2010'
'16-05-2010'
'13-06-2010'
'11-07-2010'
'08-08-2010'
'05-09-2010'
'03-10-2010'
'31-10-2010'
'28-11-2010'
'26-12-2010'
'23-01-2011'
'20-02-2011'
'20-03-2011'
'17-04-2011'
'15-05-2011'
'12-06-2011'
'12-07-2011'
'07-08-2011'
'04-09-2011'
'02-10-2011'
''
'23-11-2008'
'21-12-2008'
'18-01-2009'
'15-02-2009'
'15-03-2009'
'12-04-2009'
'10-05-2009'
'07-06-2009'
'05-07-2009'
'02-08-2009'
'30-08-2009'
'27-09-2009'
'25-10-2009'
'22-11-2009'
'20-12-2009'
'24-01-2010'
'21-02-2010'
'21-03-2010'
'18-04-2010'
'16-05-2010'
'13-06-2010'
'11-07-2010'
'08-08-2010'
'05-09-2010'
'03-10-2010'
'31-10-2010'
'28-11-2010'
'26-12-2010'
'23-01-2011'
'20-02-2011'
'20-03-2011'
'17-04-2011'
'15-05-2011'
'12-06-2011'
'12-07-2011'
'07-08-2011'
'04-09-2011'
'02-10-2011'
''
'23-11-2008'
I want to sort out a bigger matrix say mdata1 using the code
d=datenum(mdata1(:,11),'dd-mm-yyyy');
[l,idx]=sortrows(d);
zoi=mdata1(idx,:);
where mdata1(:,11), is the above column
the problem is that some cells are empty as i said before
and the matlab stucks at command datenu
Is there anything I can to avoid this problem withou creating a fake date for the empty cells?
thanks in advance

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 28일
d1 = mdata1(:,11);
d1(cellfun('isempty',d1)) = {'00-00-0000'};
[id,id] = sort(datenum(d1,'dd-mm-yyyy'));
out = mdata1(id,11);
  댓글 수: 2
antonet
antonet 2012년 5월 28일
thank you andrei
antonet
antonet 2012년 5월 28일
just a small correction. it should be out = mdata1(id,:);

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2012년 5월 27일
What do you want to have happen for those empty locations?
You can use
is_empty_date = cellfun(@isempty, mdata1(:,11));
and then is_empty_date would be a logical array telling you which were empty or not. You could use that to remove the empties or to move them to the beginning or the end (and you would sortrows() only on the non-empties)
  댓글 수: 2
antonet
antonet 2012년 5월 27일
tank you walter. So you mean that I can not combine your command with mine to sort out the matrix mdata1 according to 11th column that, in my case should also include the empties.
thanks
Oleg Komarov
Oleg Komarov 2012년 5월 27일
Yes you can, just use the negated is_empty_date to select those which are NOT empty, sort them. Then use is_empty_date to select the empty ones and concatenate with teh sorted at the end or at the beginning.

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


the cyclist
the cyclist 2012년 5월 27일
I encounter this same issue frequently, and don't have a great solution. What I typically do is something like
dateCell = mdata1(:,11);
d = nan(size(dateCell));
nullIndex = strcmp(dateCell,''); % Or whatever character string works.
validIndex = not(nullIndex);
d(validIndex) = datenum(dateCell(validIndex),'mm-dd-yyyy');
Then, you'll have NaNs for the empty dates.
I hope someone has a better solution, because this is ugly! But it works.
  댓글 수: 2
antonet
antonet 2012년 5월 28일
thank you cyclist
antonet
antonet 2012년 5월 28일
your approach is ok as well

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


Geoff
Geoff 2012년 5월 28일
You don't have to use datenum to sort this data - I find datenum too slow and I generally avoid it. I prefer to represent my textual dates as YYYY-MM-DD because they're ASCII-sortable and readable.
d = char(mdata1(:,11));
[~, idx] = sortrows(d(:, [7:10,3:6,1:2])); % DD-MM-YYYY => YYYY-MM-DD
zoi = mdata1(idx, :);
  댓글 수: 2
antonet
antonet 2012년 5월 28일
Thank you Geoff
antonet
antonet 2012년 5월 28일
your approach is ok as well

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

카테고리

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