Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete repeated characters in strings in cell array

I have a cell array like this :

Input = {'CEEEGH';'CCEEG';'ABCDEFF';'BCFGG';'BCDEEG';'BEFFH';'AACEGH'}

How can I delete all of the repeated characters and just keep only 1 character left in each string in the Input ? The expected output should be like this:

Output = {'CEGH';'CEG';'ABCDEF';'BCFG';'BCDEG';'BEFH';'ACEGH'}
like image 758
kgk Avatar asked Jan 09 '23 00:01

kgk


1 Answers

use :

cellfun(@unique,input,'UniformOutput',0)



ans = 

'CEGH'
'CEG'
'ABCDEF'
'BCFG'
'BCDEG'
'BEFH'
'ACEGH'

EDIT:

To conserve ordering in case the letters are not sorted, as @thewaywewalk commented, you can use:

cellfun(@(x) unique(x,'stable'),input,'UniformOutput',0)
like image 92
bla Avatar answered Jan 15 '23 06:01

bla