[Numpy-discussion] problem with view() and strided arrays?

Robert Kern robert.kern@gmail....
Thu Apr 24 23:25:10 CDT 2008

On Thu, Apr 24, 2008 at 10:53 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:

> Views only work for items of the same size. You are trying to view bytes as
> two byte values. View is like strict pointer aliasing in C.

No, it works just fine if you have a contiguous array.

In [5]: from numpy import *

In [6]: a = ones(10, dtype=uint8)

In [7]: a
Out[7]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8)

In [8]: b = a.view(uint16)

In [9]: b
Out[9]: array([257, 257, 257, 257, 257], dtype=uint16)

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

More information about the Numpy-discussion mailing list