0

Hi,

I created a numpy matrix(1 row, 3 cols) with the following values [4 6 8]. What I want to do is to concatenate all these values into a single entry of binary values. So instead of [4 6 8] I would want to have a numpy matrix of just 1 entry [010001101000]. I would like each number to be represented by a 4 digit binary number. I looked at reshape for numpy but am still unsure how to do this? Would appreciate it if anyone could help.

Thanks

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by pyTony
0
>>> import numpy
>>> m = numpy.array([4, 6, 8])
>>> m
array([4, 6, 8])
>>> print m[-1]+16*m[-2]+16**2*m[-3]
1128
>>> sum(m*[16**2, 16, 1])
1128
>>> bin(1128)
'0b10001101000'
>>>

Edited by pyTony: n/a

0

Ok, thanks that makes sense. Now, what if I have a numpy array as follows.

m = numpy.array([0xfe,0xff,0xf7,0xff,0x65,0xef,0xcc,0xff])

I want to be able to converge this numpy array into one entry. So I would like to have one entry of 64 bits for m. I am having trouble left shifting to get the correct value. I am wondering if there is some limitation to left shifting over so many places? Is there a better way of doing this for so many bits? Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.