在 Octave 中,我想将字符串转换为字符串矩阵。假设我有一个字符串:
s = "one two three one one four five two three five four"
我想把它拆分成一个矩阵,这样它看起来像:
one
two
three
four
five
删除了重复项。
这段代码:
words = strsplit(s, ",") %Split the string s using the delimiter ',' and return a cell string array of substrings
只是创建一个矩阵 words
到与 s
完全相同。
如何将我的字符串转换为唯一单词矩阵?
最佳答案
以下也将实现这一点:
unique(regexp(string, '[A-z]*', 'match'))
或者,或者,
unique(regexp(s, '\s', 'split'))
与Werner的方案基本相同,只是省去了一个临时的,在需要进行更复杂的匹配时更加灵活。
关于matlab - octave/matlab - 将字符串转换为唯一单词矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17576191/