Decimal matrix to binary matrix correspond to each value of decimal matrix.
Show older comments
Hello,
I am having difficulties in converting a matrix of decimal numbers to its corresponding binary matrix. Here is the problem.
For example, we have a Matrix A;
A=[9 7 15 ; 4 14 8 ]
The range of values in matrix A is from 0 to 63 inclusive (64 possibility). A is in fact an 64 by 3 (64 rows by 3 columns).
First column is representing the sequential number of each row organizedly this means first row is 0 , second row is 1 , third rows is 2 ...etc till 64 row and its number is 63.
Second column each decimal number of its values ranges from 0 to 63 .
Third column each decimal number of its values ranges from 0 to 63 .
Accoding to what said above , the A matrix is an integer matrix(64x3) that it's like this : (once again first columns values are the sequential number of each row till number 63):
0 2 3
1 3 4
2 5 6
.....
.....
etc
.....
63 5 6
so what I need is to convert this matrix to binary matrix for each value this means that lets assume that I have one row of that matrix like [0 2 3]
so corresponded binary row matrix is (6bits for each value because in my case I have 64 possibility): [000000 000010 000011] .
that's just one row of matrix A, but I need to do this for all the matrix values to convert them to binary values as what I did above.
to clear more, another examples (lets assume I have matrix 2x3 - in my case once again I have matrix 64x3) which it's :
b=[0 0 1 ; 0 2 3] and as I said each binary value is consist of 6 bits because there's 64 possibilities. so that is 6 bits per decimal number.
so the corresponded matrix in binary to it for each corresponded value of matrix b is : [000000 000000 000001 ; 000000 000010 000011] .
Any help please how I can do that in matlab? I tried to use de2bi but didnt give me correct output as what I mentioned above.
Can you please help me out?
Hope to hear from you very soon.
Thank you in advance.
Regards.
2 Comments
James Tursa
on 11 Nov 2020
What do you want as a result? A char matrix? A cell array of strings? Or ... ???
Walter Roberson
on 11 Nov 2020
A 64 by (6*3) array of characters? A 64 by (6*3) array of values that are 0 or 1?
Answers (6)
b=[0 0 1 ; 0 2 3];
reshape( string(dec2bin(b,6)),size(b,1),[])
1 Comment
Matt J
on 11 Nov 2020
or
>> cell2mat( cellstr( reshape( string(dec2bin(b,6)),size(b,1),[]) ) )
ans =
2×18 char array
'000000000000000001'
'000000000010000011'
or
>> cell2mat( cellstr( reshape( string(dec2bin(b,6)),size(b,1),[]) ) ) - '0'
ans =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1
James Tursa
on 11 Nov 2020
Edited: James Tursa
on 11 Nov 2020
Maybe this is what you want:
>> A=[9 7 15 ; 4 14 8 ]
A =
9 7 15
4 14 8
>> cell2mat(arrayfun(@(x)dec2bin(x,6),A,'uni',false'))
ans =
2×18 char array
'001001000111001111'
'000100001110001000'
Or if you want numbers instead of characters
>> cell2mat(arrayfun(@(x)dec2bin(x,6),A,'uni',false'))-'0'
ans =
Columns 1 through 14
0 0 1 0 0 1 0 0 0 1 1 1 0 0
0 0 0 1 0 0 0 0 1 1 1 0 0 0
Columns 15 through 18
1 1 1 1
1 0 0 0
Or if you want string results
>> string(arrayfun(@(x)string(dec2bin(x,6)),A,'uni',false'))
ans =
2×3 string array
"001001" "000111" "001111"
"000100" "001110" "001000"
If you don't want the leading 0's
>> string(arrayfun(@(x)string(dec2bin(x)),A,'uni',false'))
ans =
2×3 string array
"1001" "111" "1111"
"100" "1110" "1000"
Or if you want decimal numbers that just "look" like binary numbers:
>> str2double(arrayfun(@(x)dec2bin(x),A,'uni',false'))
ans =
1001 111 1111
100 1110 1000
2 Comments
Jimmy cho
on 11 Nov 2020
James Tursa
on 11 Nov 2020
Edited: James Tursa
on 11 Nov 2020
See my latest edit. Does the str2double method do what you want? Note that this is just a matrix of numbers that "look" like binary digits ... they are actually decimal digits and will be a source of irritation if you try to use them as binary digits downstream in your code.
Walter Roberson
on 11 Nov 2020
bits = 6;
lookup = dec2bin(0:63,bits) - '0';
b = reshape(lookup(A+1,:).', size(A,2)*bits,[]).';
For example,
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 1
0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 1 0
corresponding to
0 2 3
1 3 4
2 5 6
1 Comment
Jimmy cho
on 11 Nov 2020
2 Comments
James Tursa
on 11 Nov 2020
Edited: James Tursa
on 11 Nov 2020
You have been given both char results and numeric results. Why don't the numeric result answers work for you? And what does it mean to have integer 0 or 1 results "with a space" between if you are not talking about char or string results???
Please show us the exact MATLAB variable you want as a result for your example.
Jimmy cho
on 11 Nov 2020
Jimmy cho
on 11 Nov 2020
0 votes
8 Comments
James Tursa
on 11 Nov 2020
This doesn't tell us anything new. We have already given you the methods to produce char or numeric results that match this. Maybe you can tell us how you intend to use this downstream in your code? Maybe that will tell us what we need to know to help you. Right now I feel like the discussion is going in circles.
Walter Roberson
on 11 Nov 2020
Where you show 110011 are you intending that that is somehow a binary value, a vector of bits 1, 1, 0, 0, 1, 1 ? If so then to store it in a single location like that you would have to use a cell array.
Or are you intending that where you show 110011 that even though you calculated the values from binary 1, 1, 0, 0, 1, 1, that what you want is the decimal number 110011 (one hundred ten thousand, and eleven) ? That can be stored in a single location, but it is generally a nuisance to compute with.
Jimmy cho
on 12 Nov 2020
Jimmy cho
on 12 Nov 2020
Walter Roberson
on 12 Nov 2020
00 00 11
Do you mean you want
{'00' '00' '11'}
Do you mean you want
{[0 0] [0 0] [1 1]}
Do you mean that you want
[0*10+0, 0*10+0, 1*10*1]
which would be decimal zero then zero then eleven, and would display as
[0 0 11]
unless it was specifically formatted?
Focusing on the 11 for a second : what would you expect class() and size() of that 11 location to be? In words what would you expect for the decimal value stored there?
Note that MATLAB does not have a bit-addressable data type that would display as bits.
I just had the thought that you could probably construct a categorical value that would display as 11 without quotes but which had the associated decimal value 3. The user would not be able to directly enter values in that format though.
There is no numeric matrix data-type in Matlab that will display its contents in bit-form. Why does it not work for you to have them as strings, as in my original answer?
A=[1,1,1 ; 0 0 0];
A_converted=reshape( string(dec2bin(A,2)),size(A,1),[])
If this does not work, it must be because you are planning to do something later with this matrix that string-type won't allow. As James urged you earlier, it would be a good idea to tell us what the later usage of the matrix A_converted will be, so that we can give appropriate advice.
Walter Roberson
on 12 Nov 2020
>> A=[00 01 00 ; 11 11 11]
the output as you see a matrix of binary ..like this my result should be !
No, it is not. The output is a matrix of decimal values, zero one zero; eleven eleven eleven .
With new enough MATLAB you can enter
A = [0b00 0b01 0b00 ; 0b11 0b11 0b11]
A =
2×3 uint8 matrix
0 1 0
3 3 3
but you cannot have values automatically displayed in binary form.
I just want to be sure you are aware, Matlab is capable of doing bitwise operations on matrices (using bitor, bitand, etc... ) even if the matrices are not displayed to you in binary form at the command line or in the variable editor. For example, you can do bitwise XOR operations on ordinary matrices with bitxor like in the following,
A=[0,1;
2,3];
B=[1,0;
3,1];
C=bitxor(A,B);
To display the input/output in binary form, you can use techniques we've already showed you:
bitstr=@(z)reshape( string(dec2bin(z,2)),size(z));
Abits=bitstr(A);
Bbits=bitstr(B);
Cbits=bitstr(C);
Abits,Bbits,Cbits
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

