[Numpy-discussion] Fast and efficient way to convert an array into binary

Gregor Thalhammer gregor.thalhammer at gmail.com
Tue Nov 18 15:42:48 EST 2008


frank wang schrieb:
> Hi,
>  
> I have a large array and I want to convert it into a binary array. For 
> exampe, y=array([1,2,3]), after the convertion I want the result 
> array([0,0,0,1,0,0,1,0,0,0,1,1]). Each digit is converted into 4 bits 
> in this example. In my real problem I want to convert each digit to 8 
> bits. My data is numpy.ndarray and the shape is, say, (1000,).
>  
> Are there fast and efficient solution for this?
>  
>  
 >>> a = array([1,2,3], dtype = uint8)
 >>> unpackbits(a)
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
       1], dtype=uint8)

Gregor



More information about the NumPy-Discussion mailing list