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?
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()).
bindig = '01'; bindig(a + 1).char(a+48) might be faster. (twice as fast in Octave)+'0' thing (and -'0' for the way back) is yet another thing I learned in SO :-)
bitget,bitsetand the ones mentioned there undersee also. I haven't seen a case yet, where working with the string-representation is really necessary, nevertheless people ask things like this all the time. Plus, working with strings is slower and eats more memory.