Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array of digits to a binary number

How could I convert array of digits to a binary number? For instance:

a=[1 0 1 0 1 0]

I would like to convert to a binary number

b=101010

Is it possible to do without loops?

like image 782
freude Avatar asked Oct 29 '13 15:10

freude


2 Answers

Maybe this is what you want:

char(a+'0')

Example:

>> a=[1 0 1 0 1 0]

a =

     1     0     1     0     1     0

>> char(a+'0')

ans =

101010

This works by converting each number to its ASCII code (+'0') and then converting the vector of resulting numbers to a string (char()).

like image 198
Luis Mendo Avatar answered Sep 28 '22 02:09

Luis Mendo


You can convert it to a string:

sprintf('%d',a)

which I think is the only alternative to an array of logicals.

like image 21
Dan Avatar answered Sep 28 '22 01:09

Dan