MATLAB Answers

Decimal matrix to binary matrix correspond to each value of decimal matrix.

조회 수: 4(최근 30일)
Jimmy cho
Jimmy cho 11 Nov 2020
편집: Matt J 13 Nov 2020
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

James Tursa
James Tursa 11 Nov 2020
What do you want as a result? A char matrix? A cell array of strings? Or ... ???
Walter Roberson
Walter Roberson 11 Nov 2020
A 64 by (6*3) array of characters? A 64 by (6*3) array of values that are 0 or 1?

댓글을 달려면 로그인하십시오.

답변(6개)

Matt J
Matt J 11 Nov 2020
편집: Matt J 11 Nov 2020
b=[0 0 1 ; 0 2 3];
reshape( string(dec2bin(b,6)),size(b,1),[])
ans = 2×3 string array
"000000" "000000" "000001" "000000" "000010" "000011"

  댓글 수: 1

Matt J
Matt J 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
James Tursa 11 Nov 2020
편집: James Tursa 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

Jimmy cho
Jimmy cho 11 Nov 2020
you may didn't understand me, that's not solving my problem.
@James Tursa see please my comment down in the next comment .. I attached two photos to clear more what my output should be.
James Tursa
James Tursa 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
Walter Roberson 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

Jimmy cho
Jimmy cho 11 Nov 2020
you may didn't understand me, that's not solving my problem.
I will try by an example to explain: (just for the example I used 4x3 matrix that has decimal values ..and I want to convert them to binary values with 6 bit each )
converting it to this (just for the example the binary consist of 2 bits each..but in my case is 6bits each)
here as you see for each decimal value of first matrix above corresponds in binary the value of it ..

댓글을 달려면 로그인하십시오.


Jimmy cho
Jimmy cho 11 Nov 2020
편집: Jimmy cho 11 Nov 2020
Appreciate your help guys, but apparently didn't understand me.
I dont want the binary values as characters or strings! , it's integer 0 or 1 ..
once again lets assume I have matrix A with size 1x3 (in my case it's 64x3 but just for the example) and it's [1 0 1]
so the corresponded converted binary matrix to it for each decimal value is : [000001 000000 000001] (assumed here the binary value consis of 6bits because I have values ranged from 0 to 63 which it's 64 possibility) and I need the space between each binary value it means in my example it should look like this:
[000001 000000 000001]
thanks and appreciated for any help

  댓글 수: 2

James Tursa
James Tursa 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
Jimmy cho 11 Nov 2020
attached down a photo that really can expain what I mean, what my output of my problem should be like
thanks alot for any help!

댓글을 달려면 로그인하십시오.


Jimmy cho
Jimmy cho 11 Nov 2020
Attaching down the representation of the output to my problem : (once again I have matrix 64x3 but just for understanding make it of 4x3) ..so here each decimal value converted to binary value which consist of 6 bits corresponded to each decimal value of the matrix that I convert

  댓글 수: 8

표시 이전 댓글 수: 5
Jimmy cho
Jimmy cho 12 Nov 2020
I really appreciate your effort, sorry for not clearing out my problem exactly!
what I want is like this, I write in my matlab workspace like this :
>> A=[00 01 00 ; 11 11 11]
A =
0 1 0
11 11 11
the output as you see a matrix of binary ..like this my result should be !
hope it's clear now.
thanks alot.
so I mean I input a matrix like A=[1 1 1 ; 0 0 0]; the output is a converted binary matrix (related to how number of binary bits consist , in my case 6bits) so the converted matrix (the output ) is:
A_converted=
01 01 01
00 00 00
as you seee for each decimal value in matrix A converted correspondingly to binary value in A_converted..
Matt J
Matt J 12 Nov 2020
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),[])
A_converted = 2×3 string array
"01" "01" "01" "00" "00" "00"
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
Walter Roberson 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.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 12 Nov 2020
편집: Matt J 13 Nov 2020
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
Abits = 2×2 string array
"00" "01" "10" "11"
Bbits = 2×2 string array
"01" "00" "11" "01"
Cbits = 2×2 string array
"01" "01" "01" "10"

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by