How can I delete duplicates in an array, with exceptions?

I have an array which I would like to delete only the 2nd duplicate of a set in an array e.g:
'B1(46)'
'B13(201)'
'B14(201)'
'B14(201)' - delete
'B1(52)'
'B2(52)'
'B2(52)' - delete
'B5(124)'
'B6(124)'
'B6(124)' - delete
'B13(201)'
'B14(201)'
'B14(201)' - delete
My current script (below) will delete all duplicates of 'B14(201)', which is not what I want:
wd=event; % with duplicates
[~,idx]=unique(strcat(wd()),'stable');
newevent=wd(idx); %without duplicates
Thanks

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 12일
편집: Andrei Bobrov 2013년 11월 12일
A - your array:
[ii,ii,ii] = unique(A);
out = A([true;diff(ii)~=0]);

댓글 수: 7

Thanks Andrei, but I get the following error:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in MADSCRIPT3 (line 79)
out = A([true;diff(ii)~=0]);
Im not sure why...
Ok I added the following and get no error, although it does not appear to delete duplicates?
A=strvcat(event)
[ii,ii,ii] = unique(A);
out = A([true;diff(ii)~=0]);
What is your array (A in my case)?
My solution work for case:
>> A = {'B1(46)'
'B13(201)'
'B14(201)'
'B14(201)'
'B1(52)'
'B2(52)'
'B2(52)'
'B5(124)'
'B6(124)'
'B6(124)'
'B13(201)'
'B14(201)'
'B14(201)'};
>> [ii,ii,ii] = unique(A);
out = A([true;diff(ii)~=0])
out =
'B1(46)'
'B13(201)'
'B14(201)'
'B1(52)'
'B2(52)'
'B5(124)'
'B6(124)'
'B13(201)'
'B14(201)'
>>
Ok the 'event' or 'A' variable is a 1x169 cell array. The strvcat function turns it into a 169x8 char array...
Andrei Bobrov
Andrei Bobrov 2013년 11월 12일
편집: Andrei Bobrov 2013년 11월 12일
With my variant of array - working?
This works now, thank you very much.
Hi Andrei, I have sent you an email on your email address. Would me much appreciated if I can get a reply regards.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

질문:

2013년 11월 12일

댓글:

2013년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by