What's the best way to add semi-colons after each element of a matrix?

5 ビュー (過去 30 日間)
Xiaohan Du
Xiaohan Du 2018 年 3 月 21 日
編集済み: Stephen23 2020 年 5 月 28 日
Hi all,
I have a cell matrix which contains strings like this (showing part of the cell, actual cell can be quite large):
example =
3×1 cell array
{'0 0 0.1 0.0016892 0.2 0.0068476 0.3 0.0016477' }
{'0.4 -8.2671e-05 0.5 -0.00012313 0.6 -0.00016255 0.7 -0.0002006'}
{'0.8 -0.00023696 0.9 -0.00027131' }
I would like to add semi-colons after each element, so the matrix becomes:
{'0, 0, 0.1, 0.0016892, 0.2, 0.0068476, 0.3, 0.0016477,' }
{'0.4, -8.2671e-05, 0.5, -0.00012313, 0.6, -0.00016255, 0.7, -0.0002006,'}
{'0.8, -0.00023696, 0.9, -0.00027131,' }
I know I can probably write a for-loop to do it, but it seems very inefficient since I'll need to deal with each element? So what's the most efficient way to add semi-colon after each element?
Many thanks!

採用された回答

Stephen23
Stephen23 2018 年 3 月 21 日
編集済み: Stephen23 2020 年 5 月 28 日
>> D = regexprep(C,'\S+','$&,');
>> D{:}
ans = 0, 0, 0.1, 0.0016892, 0.2, 0.0068476, 0.3, 0.0016477,
ans = 0.4, -8.2671e-05, 0.5, -0.00012313, 0.6, -0.00016255, 0.7, -0.0002006,
ans = 0.8, -0.00023696, 0.9, -0.00027131,

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by