MATLAB Answers

turn a sentence into matrix of words

1 ビュー (過去 30 日間)
Kamyar Mazarei
Kamyar Mazarei 2021 年 5 月 6 日
編集済み: DGM 2021 年 5 月 6 日
hi i have A=1xn characters sentence i want to create matrix B=1xn size
which B hast words in a matrix set
thank you
edit: i think its called cell and not matrix
im not sure im new
  1 件のコメント
KSSV
KSSV 2021 年 5 月 6 日
Can you show us an example?

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2021 年 5 月 6 日
str = 'I Love MATLAB' ;
iwant = strsplit(str)
iwant = 1×3 cell array
{'I'} {'Love'} {'MATLAB'}

DGM
DGM 2021 年 5 月 6 日
編集済み: DGM 2021 年 5 月 6 日
Depends whether you want to do this with strings or character arrays:
sentence = "this is a string, not a character array";
words = split(sentence,' ') % this is an 8x1 array of strings
gives
words =
8×1 string array
"this"
"is"
"a"
"string,"
"not"
"a"
"character"
"array"
Alternatively
sentence = 'this is a character array';
words = split(sentence,' ') % this is an 5x1 cell array of chars
gives
words =
5×1 cell array
{'this' }
{'is' }
{'a' }
{'character'}
{'array' }
split() is relatively new (R2016b), but you should also be able to use strsplit() back to R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by