Search code examples
stringmatlabcell-array

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'}

Solution

  • 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)