How to convert an array of decimal values to binary?

4 views (last 30 days)
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
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?
Pablo Díaz Amores
Pablo Díaz Amores on 15 Jun 2018
Edited: Pablo Díaz Amores on 15 Jun 2018
I missed 2 zeros, you are right. The array x_bin should have 6 zeros at the beginning. I edited it so nobody gets confused. Thanks you for your help!

Sign in to comment.

Accepted Answer

Stephen23
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!