How can convert binary Numerical Values to Strings?

1 回表示 (過去 30 日間)
William Collants
William Collants 2020 年 5 月 19 日
コメント済み: Walter Roberson 2020 年 5 月 19 日
let's say I Have a Matrix M which contains only binary values of 0 and 1
M = de2bi (0:10)
How can I convert that into a Matrix which is comprised of Strings (e.g. 'black' & 'white', or 'zero' & 'one') instead of the 0's and 1's ?
0 1 0 1
0 0 0 1
0 1 0 0
1 0 0 1
to
black white black white
black black black white
black white black black
white black black white

採用された回答

Image Analyst
Image Analyst 2020 年 5 月 19 日
How about
ca = cell(size(yourMatrix)); % Preallocate cell array
for k = 1 : numel(ca)
if yourMatrix(k)
ca{k} = 'white '; % yourMatrix is 1
else
ca{k} = 'black '; % yourMatrix is 0
end
end
  2 件のコメント
William Collants
William Collants 2020 年 5 月 19 日
(just for perfectionism) any way to hide the { }, [ ] and ' ' etc in the resulting Array?
Stephen23
Stephen23 2020 年 5 月 19 日
編集済み: Stephen23 2020 年 5 月 19 日
"any way to hide the { }, [ ] and ' ' etc in the resulting Array?"
Those are not actually part of the array in memory, they are just artefacts of the display routine that MATLAB uses. If you want to display the data differently, then you can write your own display routine based on fprintf.

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

その他の回答 (1 件)

James Tursa
James Tursa 2020 年 5 月 19 日
E.g.
>> b = rand(5)<.5 % generate some sample data
b =
5×5 logical array
0 0 0 1 1
0 1 0 1 1
1 1 1 0 0
0 1 0 0 0
1 1 1 1 0
>> s = {'black','white'}; % the strings you want for 0 and 1
>> c = s(b+1)
c =
5×5 cell array
{'black'} {'black'} {'black'} {'white'} {'white'}
{'black'} {'white'} {'black'} {'white'} {'white'}
{'white'} {'white'} {'white'} {'black'} {'black'}
{'black'} {'white'} {'black'} {'black'} {'black'}
{'white'} {'white'} {'white'} {'white'} {'black'}

カテゴリ

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