Decimal matrix to binary matrix correspond to each value of decimal matrix.
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
James Tursa
el 11 de Nov. de 2020
What do you want as a result? A char matrix? A cell array of strings? Or ... ???
Walter Roberson
el 11 de Nov. de 2020
A 64 by (6*3) array of characters? A 64 by (6*3) array of values that are 0 or 1?
Respuestas (6)
Matt J
el 11 de Nov. de 2020
Editada: Matt J
el 11 de Nov. de 2020
b=[0 0 1 ; 0 2 3];
reshape( string(dec2bin(b,6)),size(b,1),[])
1 comentario
Matt J
el 11 de Nov. de 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
el 11 de Nov. de 2020
Editada: James Tursa
el 11 de Nov. de 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 comentarios
James Tursa
el 11 de Nov. de 2020
Editada: James Tursa
el 11 de Nov. de 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
el 11 de Nov. de 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
Jimmy cho
el 11 de Nov. de 2020
Editada: Jimmy cho
el 11 de Nov. de 2020
2 comentarios
James Tursa
el 11 de Nov. de 2020
Editada: James Tursa
el 11 de Nov. de 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
el 11 de Nov. de 2020
8 comentarios
Matt J
el 12 de Nov. de 2020
Editada: Matt J
el 12 de Nov. de 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),[])
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
el 12 de Nov. de 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
el 12 de Nov. de 2020
Editada: Matt J
el 13 de Nov. de 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
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!