Search code examples
matlabbinaryoctave

Decimal to binary as double type array, not string


I have this so far:

data = 14
out = dec2bin(data, 4)

which gives:

out = 1110

But I want to get binary number in this format:

out = [1 1 1 0]

Thanks for help!


Solution

  • You're looking for de2bi with the 'left-msb' option.

    data = 14
    out = de2bi(data, 4,'left-msb')
    

    Which requires the Communication Systems Toolbox though. Alternatively use your original approach with the fundamental dec2bin with the following addition:

    data = 14
    out = double( dec2bin(data, 4) ) - 48
    

    out =
    
         1     1     1     0