How to convert an array of decimal values to binary?
4 views (last 30 days)
Show older comments
Pablo Díaz Amores
on 15 Jun 2018
Edited: Pablo Díaz Amores
on 15 Jun 2018
Hi everyone,
I have a very large array read from a file:
x = [0 0 0 2 0 3 0 1 0 0 ....]'
This values correspond to symbols from QPSK demodulation and I need to convert the array to binary so that the result is:
x_bin = [0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 ....]'
I tried using x_bin = cellstr(dec2bin(x,2)) but what I get is this cell array with 2 bits in each position:
x_bin = [00 00 00 10 00 11 00 01 00 00 ...]'
I need a numerical array (not a cell array) with 1 bit in each position like the one above.
Is there any way to achieve this?
Thank you in advance!
2 Comments
Stephen23
on 15 Jun 2018
@Pablo Díaz Amores: your x_bin examples seem to be missing the first pair of zeros: x has three leading zeros, so surely x_bin should have six leading zeros?
Accepted Answer
Stephen23
on 15 Jun 2018
>> x = [0 0 0 2 0 3 0 1 0 0];
>> c = dec2bin(x(:),2);
>> reshape(c.',1,[])-'0'
ans =
0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0
0 Comments
More Answers (0)
See Also
Categories
Find more on String Parsing 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!