[Numpy-discussion] converting 1 dimensional array to 2 dimensional array

Omry Levy omrylevy at gmail.com
Mon Jul 15 13:43:50 EDT 2019


Hi All,

I know how to reshape arrays, my problem is a little more complicated than
that.
I am looking for the most efficient way to do the following and an example
will help:

1) I have a an array of bytes   b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
This bytes array represents a width of 2 and a height of 3.
Each 2 bytes in that bytes array makes an unsigned 16 bit integer.

2) I want to get the following 2 dimensional array from the bytes array
where each item in the two dimensional array is an unsigned 16 bit integer
something like:

[
      [(1,2)    , (3,4)],
      [(5,6)    , (7,8)],
      [(9,10)  , (11,12)]
]

in the first row there are 2 elements each one is made of 2 bytes from the
bytes array etc...
meaning that
(1, 2) =  b[0] + b[1] << 8
(3, 4) =  b[2] + b[3] << 8
...
...

What is the most efficient way to achieve that with numpy ?
Thanks
0L






[1] << 8 ,[2], [3, 4]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190715/a747e7c9/attachment.html>


More information about the NumPy-Discussion mailing list