找到你要的答案

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

Q:创建字符串不重复相同的元素在字符串(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"

我有一个字符串“fdfaccfffbdcgghbbcfge”。谁能帮助我生成一个新的字符串,但没有重复两次的顺序相同的顺序。谢谢!

预期的输出应该是这样的:“fdacbghe”

answer1: 回答1:

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

采用独特的“stable'option:

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

如果你想要更多的手册:使用bsxfun建立没有出现元素的逻辑索引(~任何(…))早些时候(triu(…,1)):

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