octave / matlab-将字符串转换为唯一单词的矩阵

Oba*_*bay 4 matlab vector matrix octave

在八度中,我想将字符串转换为字符串矩阵。说我有一个字符串:

s = "one two three one one four five two three five four"
Run Code Online (Sandbox Code Playgroud)

我想将其拆分为矩阵,使其看起来像:

one
two
three
four
five
Run Code Online (Sandbox Code Playgroud)

删除重复项。

这段代码:

words = strsplit(s, ",") %Split the string s using the delimiter ',' and return a cell string array of substrings
Run Code Online (Sandbox Code Playgroud)

只需创建一个words与完全相同的矩阵即可s

如何将字符串转换为唯一单词的矩阵?

Rod*_*uis 5

以下也将完成此操作:

unique(regexp(string, '[A-z]*', 'match'))
Run Code Online (Sandbox Code Playgroud)

或者,

unique(regexp(s, '\s', 'split'))
Run Code Online (Sandbox Code Playgroud)

基本上与Werner的解决方案相同,但是它可以节省临时时间,并且在需要进行更复杂的比赛时更加灵活。