repeated characters in string

11 Ansichten (letzte 30 Tage)
Ebtesam Almansor
Ebtesam Almansor am 13 Okt. 2016
Beantwortet: Gautam Mohan am 18 Okt. 2016
Hi there, I want to delete any character which repeated more than 3 in string I have this code which delete all repatriation Input = {'CEEEGH';'CCEEG';'ABCDEFF';'BCFGG';'BCDEEG';'BEFFH';'AACEGH'}
cellfun(@unique,Input,'UniformOutput',0)
  2 Kommentare
James Tursa
James Tursa am 13 Okt. 2016
Delete characters that repeat more than 3 times total, or more than 3 times in a row?
Jos (10584)
Jos (10584) am 13 Okt. 2016
What is the output supposed to look like?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Gautam Mohan
Gautam Mohan am 18 Okt. 2016
Hi Ebtesam,
If you want to delete any character which is repeated three or more times in a row, you can substitute the @unique function in your cellfun() with a regexp that searches for 3+ character matches and replaces them with nothing:
f = @(s) regexprep(s, '(\S)\1\1+', '');
cellfun(f,Input,'UniformOutput',0)
If you need to eliminate characters that occur 3 or more times throughout the whole string, I would recommend writing a separate function that accomplishes that goal and then applying it to each string using cellfun().
Hope this helps!

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by