How to convert decimal matrix matrix into binary matrix?
20 次查看(过去 30 天)
显示 更早的评论
hi
I want to convert 3 by 4 decimal matrix into equivalent 3 by 4 binary matrix
e.g
a=[10 9 8 7
9 7 9 6
1 2 3 8]
b=[1010 1001 1000 0111
1001 0111 1001 0110
0001 0010 0011 1000]
kindly help me I will be highly thankful to you
regards, mudasir
0 个评论
采纳的回答
Guillaume
2016-10-23
Note there's not such thing as
b=[1010 1001 1000 0111
1001 0111 1001 0110
0001 0010 0011 1000]
in matlab. Binary matrices don't exist in matlab.
The closest you can get is a cell array of string, which you'll obtain with:
a = [10 9 8 7
9 7 9 6
1 2 3 8]
b = reshape(cellstr(dec2bin(a)), size(a))
which produces:
b =
3×4 cell array
'1010' '1001' '1000' '0111'
'1001' '0111' '1001' '0110'
'0001' '0010' '0011' '1000'
2 个评论
Guillaume
2016-10-23
a = reshape(bin2dec(b), size(b))
to get the original a back.
As far as matlab is concerned, there's nothing binary about b, they're just strings (that happen to contain 0 and 1). You can of course manipulate strings and replace characters:
c = cellfun(@(s) [s(1:3), '1'], b, 'UniformOutput', false);
But if all you want to do is change a bit, then you're better off staying with the original decimal array and use bitset:
bitset(a, 1, 1) %set bit 1 to 1
更多回答(1 个)
Andriy Kavetsky
2016-10-23
Use dec2base(a,2) command to get string array of binary numbers and str2num(dec2base(a,2)) to convert string into numbers
3 个评论
Walter Roberson
2016-10-23
a=[10 9 8 7
9 7 9 6
1 2 3 8];
dec2base(a)
gives a char array as output.
To convert back you need to use base2dec()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!