4

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

1 Answer 1

9

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Beat me to it, came up with exactly the same.
Best way to go, but I'd optionally consider the 'stable' property of unique to keep the order in case the letters of the strings are not sorted.
@thewaywewalk How do you specify name-value pairs to function handles? I don't think I've seen it done before and the function handle documentation is pretty sparse.
@excaza just use cellfun(@(x) unique(x,'stable'),input,'UniformOutput',0) - directly in the handle is not possible, that's right.
@thewaywewalk darn, I was hoping for some new syntactic sugar :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.