Unique changing order(unique output values are reshuffled)

조회 수: 7 (최근 30일)
shaz
shaz 2012년 12월 18일
댓글: Stephen23 2018년 1월 17일
i have cell array with data like '2/6/2009','2/6/2009','2/6/2009','2/6/2009','2/7/2009',,'2/7/2009','2/7/2009','2/8/2009','2/8/2009','2/8/2009'
if i make use of unique to get only unique values the order is getting reshuffled
getting output like : '2/8/2009','2/6/2009','2/7/2009'
Desired output format: '2/6/2009','2/7/2009','2/8/2009'
  댓글 수: 1
Stephen23
Stephen23 2018년 1월 17일
Simply using my FEX submission natsort:
>> C = {'2/6/2009','2/6/2009','2/6/2009','2/6/2009','2/7/2009','2/7/2009','2/7/2009','2/8/2009','2/8/2009','2/8/2009'};
>> D = natsort(unique(C));
>> D{:}
ans = 2/6/2009
ans = 2/7/2009
ans = 2/8/2009
But the best solution is to change the date format for an ISO 8601 date format, which sort correctly into chronological order when you do a character sort. Once you start using ISO 8601 date formats you simply avoid all of these trivial problems.

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

채택된 답변

Muruganandham Subramanian
Muruganandham Subramanian 2012년 12월 18일
b={'2/6/2099','2/6/2099','2/6/2099','2/6/2099','2/7/2099','2/7/2099', '2/7/2099','2/8/2099','2/8/2099','2/8/2099','2/10/2099','2/10/2099', '2/12/2099'};
c=datevec(b);
c(:,4:6)=[];
c(:,4)=c(:,1);
c(:,1)=[];
d=num2str(c);
d=cellstr(d);
d=unique(d)
Check this above code. But it's not effective way to do..here In unique(), it's not chosing in random way, it considers the data of first,which is sorted minimum(i.e. '10' is first than '6').
  댓글 수: 4
Andrei Bobrov
Andrei Bobrov 2012년 12월 19일
M = datevec(b,'mm/dd/yyyy');
[Muq, ii] = unique(M,'rows','first');
out = b(sort(ii));
shaz
shaz 2012년 12월 31일
thanks a lot

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

추가 답변 (4개)

Muruganandham Subramanian
Muruganandham Subramanian 2012년 12월 18일
편집: Muruganandham Subramanian 2012년 12월 18일
>>b={'2/6/2009','2/6/2009','2/6/2009','2/6/2009','2/7/2009','2/7/2009','2/7/2009',
'2/8/2009','2/8/2009','2/8/2009'};
>> c=unique(b)
>> c =
'2/6/2009' '2/7/2009' '2/8/2009'
Are you expecting this?
  댓글 수: 1
shaz
shaz 2012년 12월 18일
b={'2/6/2099','2/6/2099','2/6/2099','2/6/2099','2/7/2099','2/7/2099', '2/7/2099','2/8/2099','2/8/2099','2/8/2099','2/10/2099','2/10/2099', '2/12/2099'};
c=unique(b)
the output is
c= '2/10/2099' '2/12/2099' '2/6/2099' '2/7/2099' '2/8/2099'
which is in a random way

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


Jan
Jan 2012년 12월 18일
With a modern Matlab version you can use:
c = unique(b, 'first');

Sebastian
Sebastian 2018년 1월 17일
I know it's a bit late for an answer, but maybe it helps others. If you want to preserve the order of the input of 'unique', you can use the second return parameter. Like that it works for arbitrary data (i.e. strings and numerals, basically every data type 'unique' supports):
a = {'2/6/2009' '2/6/2009' '2/6/2009' '2/7/2009' '2/7/2009' '2/7/2009' '2/8/2009' '2/8/2009' '2/8/2009'};
[~, uIdx] = unique(a);
a(sort(uIdx))
This snippet does the following: Store the indices of the unique elements of 'a' in 'uIdx'. Then return the elements of 'a' from the first to the last using 'sort'.
  댓글 수: 1
Jan
Jan 2018년 1월 17일
The problem of shaz was, that he wanted a specific numerical order, in which '6' appears before '10'. Therefore the data must be converted from string to double, such that the sorting works.

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


Steven Lord
Steven Lord 2018년 1월 17일
Convert your date data into a datetime array, then call unique (with or without the 'stable' flag) on the datetime array.
C = {'2/6/2009','2/6/2009','2/6/2009','2/6/2009','2/7/2009','2/7/2009', ...
'2/7/2009','2/10/2009','2/8/2009','2/8/2009'};
D = datetime(C, 'InputFormat', 'MM/dd/uuuu')
unique(D)
unique(D, 'stable')
For purposes of this example I assumed your dates were days in February 2009. If they were the second of each month from June through October (without September) of 2009 swap the MM and dd sections of the InputFormat parameter.
  댓글 수: 1
Stephen23
Stephen23 2018년 1월 17일
"For purposes of this example I assumed your dates were days in February 2009. If they were the second of each month from June through October (without September) of 2009 swap the MM and dd sections of the InputFormat parameter."
Or avoid this pointless and confusing ambiguity entirely by using ISO 8601 dates.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by