Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Decimal to Binary Vector

Tags:

binary

matlab

I need to convert a Decimal Number to a Binary Vector

For example, Something like this:

  length=de2bi(length_field,16);

Unfortunately, because of licensing, I cannot use this command. Is there any quick short technique for converting binary to a vector.

Here is what I am looking for,

If 
Data=12;
Bin_Vec=Binary_To_Vector(Data,6) should return me
Bin_Vec=[0 0 1 1 0 0]

Thanks

like image 992
Kiran Avatar asked Apr 21 '11 13:04

Kiran


People also ask

How do I convert decimal to binary?

An easy method of converting decimal to binary number equivalents is to write down the decimal number and to continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0” until the final result equals zero. So for example. Convert the decimal number 29410 into its binary number equivalent.

What does dec2bin do in Matlab?

str = dec2bin( d ) returns the binary representation of symbolic number d as a character vector. d must be a nonnegative integer. If d is a matrix or multidimensional array of symbolic numbers with N elements, dec2bin returns a character array with N rows.


1 Answers

A single call to Matlab's built-in function dec2bin can achieve this:

binVec = dec2bin(data, nBits)-'0'
like image 146
jasxun Avatar answered Oct 23 '22 04:10

jasxun