Binary string to character conversion

How can i assign 00 to -3d, 01 to -d, 11 to +d, 10 to +3d if i have a binary string x = char('0' + (rand(1, 1000) < 0.5))?

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 19 日
編集済み: Azzi Abdelmalek 2016 年 4 月 19 日

0 投票

x = char('0' + (rand(1, 1000) < 0.5))
a={'-3d' '-d' '+d' '+3d'}
idx=bin2dec(reshape(x,2,[])')+1
out=a(idx)
If you want to join them
out=strjoin(a(idx),'')

4 件のコメント

Ying Wai Wong
Ying Wai Wong 2016 年 4 月 19 日
I am new to Matlab. What is idx mean? There is idx = 231412344.....etc
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 19 日
編集済み: Azzi Abdelmalek 2016 年 4 月 19 日
There is an error;
a={'-3d' '-d' '+3d' '+d'}
You have
00 corresponds to index idx=1
01 corresponds to index idx=2
10 corresponds to index idx=3
11 corresponds to index idx=4
Ying Wai Wong
Ying Wai Wong 2016 年 4 月 19 日
ahh yes, cheers mate! and if i want to change d to 1 how can i do it?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 19 日
strrep(out,'d','1')

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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