kgk
kgk

Reputation: 659

Create string without repeating the same element jn the string (Matlab)

I have a string "FDFACCFFFBDCGGHBBCFGE" . Could anyone help me to generate a new string with the same order but no element inside repeated twice. Thanks !

The expected output should be like this : "FDACBGHE"

Upvotes: 1

Views: 47

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112749

Use unique with the 'stable'option:

str = 'FDFACCFFFBDCGGHBBCFGE';
result = unique(str, 'stable');

If you want something more manual: use bsxfun to build a logical index of the elements that haven't appeared (~any(...)) earlier (triu(..., 1)):

result = str(~any(triu(bsxfun(@eq, str, str.'), 1)));

Upvotes: 6

Related Questions