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

Recommended Answers

All 4 Replies

You could matrix multiply on the right by matrix with 3 rows and 1 column [16*16, 16, 1].

>>> 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'
>>>

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.

I think you should be using struct module.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.