フィルターのクリア

How to replace matrix value with string from cell array?

2 ビュー (過去 30 日間)
Johnny Birch
Johnny Birch 2018 年 10 月 1 日
コメント済み: Johnny Birch 2018 年 10 月 2 日

I have a cell array of string and a data matrix. How do I replace the value in the data matrix with the strings marked by ‘#’. I want the output to be a cell array of strings like the “output” in the script below.

cell = {
    '# 2.537'    '1.219'      '0.457'    '0.214'    '# 0.120'    '0.245'
    '3.244'      '# 1.400'    '0.649'    '0.515'    '0.207'      '0.075'
    '2.993'      '1.282'      '0.605'    '0.309'    '0.140'      '0.140'
    '3.278'      '1.507'      '0.885'    '0.405'    '0.160'      '0.111'
    '3.691'      '1.474'      '0.650'    '0.316'    '0.180'      '0.155'}
data=[
    0.8147    0.0975    0.1576    0.1419    0.6557    0.7577
    0.9058    0.2785    0.9706    0.4218    0.0357    0.7431
    0.1270    0.5469    0.9572    0.9157    0.8491    0.3922
    0.9134    0.9575    0.4854    0.7922    0.9340    0.6555
    0.6324    0.9649    0.8003    0.9595    0.6787    0.1712]
output={
    '# 2.537'   '0.0975'    '0.1576'    '0.1419'    '# 0.120'   '0.7577'
    '0.9058'    '# 1.400'   '0.9706'    '0.4218'    '0.0357'    '0.7431'
    '0.1270'    '0.5469'    '0.9572'    '0.9157'    '0.8491'    '0.3922'
    '0.9134'    '0.9575'    '0.4854'    '0.7922'    '0.9340'    '0.6555'
    '0.6324'    '0.9649'    '0.8003'    '0.9595'    '0.6787'    '0.1712'}

採用された回答

Jan
Jan 2018 年 10 月 2 日
output = sprintfc('%.4f', data);
index = strncmp(c, '#', 1);
output(index) = c(index);
  1 件のコメント
Johnny Birch
Johnny Birch 2018 年 10 月 2 日
Brilliant, thanks a lot Jan.

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

その他の回答 (1 件)

jonas
jonas 2018 年 10 月 1 日
編集済み: jonas 2018 年 10 月 1 日
Cell is a built in function so avoid using it as a variable name. Anyway, you can try this:
mask=logical(count(cell,'#'))
data=cellstr(string(a))
data(mask)=cell(mask)
data =
5×6 cell array
{'# 2.537'} {'0.0975' } {'0.1576'} {'0.1419'} {'# 0.120'} {'0.7577'}
{'0.9058' } {'# 1.400'} {'0.9706'} {'0.4218'} {'0.0357' } {'0.7431'}
{'0.127' } {'0.5469' } {'0.9572'} {'0.9157'} {'0.8491' } {'0.3922'}
{'0.9134' } {'0.9575' } {'0.4854'} {'0.7922'} {'0.934' } {'0.6555'}
{'0.6324' } {'0.9649' } {'0.8003'} {'0.9595'} {'0.6787' } {'0.1712'}
  1 件のコメント
Johnny Birch
Johnny Birch 2018 年 10 月 1 日
編集済み: Johnny Birch 2018 年 10 月 1 日
Hi Jonas, thanks a lot for your answer. I changed the "cell" to "c", but I get this error:
Undefined function 'count' for input arguments of type 'cell'.
mask=logical(count(c,'#'))

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

カテゴリ

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

タグ

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by