フィルターのクリア

Splitting a cell array of multi-word strings into a cell array of single-word strings

7 ビュー (過去 30 日間)
I have a cell array of multi-word strings that is very long (many tens of thousands of cells) that I want to split into a cell array of single-word strings. Is there a way to do this without combining the split function and a for loop?
Currently, I am doing the following:
CellStrings = {'Here is my First String';'Now a second string';'And here is a third'}
SingleColumnStrings = {};
for i = 1:length(CellStrings)
temp = split(CellStrings(i));
SingleColumnStrings = [SingleColumnStrings; temp];
clear temp
end
clear i
When CellStrings gets large, this for loop takes forever. Is there a way to do this as a matrix/vector operation?
Thanks in advance.
  1 件のコメント
Guillaume
Guillaume 2020 年 2 月 3 日
Note that clear is rarely needed. clear inside a loop is a particularly bad idea and will slow the loop for no useful purpose.

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

採用された回答

fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 3 日
I think this can do nicely:
SingleColumnStrings=cellstr(strsplit(strjoin(string({'Here is my First String';'Now a second string';'And here is a third'})'))')
  1 件のコメント
Illan Kramer
Illan Kramer 2020 年 2 月 3 日
編集済み: Illan Kramer 2020 年 2 月 3 日
That's perfect! My tic/toc runtime now for that operation has gone from over 50s to about 0.25s. Thanks so much! I actually just put the transpose on the outside of the entire right side of the equal sign instead of having 2 of them in there and it worked just as well.

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

その他の回答 (1 件)

Guillaume
Guillaume 2020 年 2 月 3 日
編集済み: Guillaume 2020 年 2 月 3 日
Possiblty more efficient than the accepted answer since it doesn't require concatenating strings to then split them again:
SingleColumnStrings = regexp(CellStrings, '\S+', 'match');
SingleColumnStrings =[SingleColumnStrings{:}].';
  2 件のコメント
Stephen23
Stephen23 2020 年 2 月 3 日
+1 definitely the way to go.
Illan Kramer
Illan Kramer 2020 年 2 月 3 日
This is also a great solution, thanks! Comparing tic/toc runtimes, this one is 0.01s faster than the accepted answer. I will luxuriate in all of my new spare time upon deploying this solution going forward.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by