Reputation: 659
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
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