octave/matlab-将字符串转换为唯一单词的矩阵 [英] octave/matlab - convert string into matrix of unique words

查看:154
本文介绍了octave/matlab-将字符串转换为唯一单词的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Octave, I want to convert a string into a matrix of strings. Say I have a string:

s = "one two three one one four five two three five four"

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

I want to split it into a matrix so that it looks like:

one
two
three
four
five

已删除重复项.

此代码:

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

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

Just creates a matrix words into exactly the same as s.

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

How to I convert my string into a matrix of unique words?

推荐答案

以下内容也可以实现这一点:

The following will also accomplish that:

unique(regexp(string, '[A-z]*', 'match'))

或者,或者,

unique(regexp(s, '\s', 'split'))

与Werner的解决方案基本相同,但是它节省了临时时间,并且在需要进行更复杂的匹配时更加灵活.

Basically the same as Werner's solution, but it saves a temporary and is more flexible when more complicated matches need to be made.

这篇关于octave/matlab-将字符串转换为唯一单词的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆