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

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 12 Nov. 2013
Bearbeitet: Andrei Bobrov am 12 Nov. 2013
A - your array:
[ii,ii,ii] = unique(A);
out = A([true;diff(ii)~=0]);

7 Kommentare

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 am 12 Nov. 2013
Bearbeitet: Andrei Bobrov am 12 Nov. 2013
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by