I am looking at the answer to this question and can't wrap my head around how the as_strided
function is viewing this array.
This piece of code is part of the answer:
>>> a = np.lib.stride_tricks.as_strided(np.array([1, 512, 0, 3], dtype=np.int16),
shape=(3,), strides=(3,))
>>> a
array([1, 2, 3], dtype=int16)
>>> a.strides[0]
3
>>> a.itemsize
2
Assuming each element of the passed array is 2 bytes long, we have the following byte representation of the array:
-------------------------------------------------------------------------------------
1 | 512 | 0 | 3
-------------------------------------------------------------------------------------
0000 0000 0000 0001 | 0000 0010 0000 0000 | 0000 0000 0000 0000 | 0000 0000 0000 0011
So, considering each element to be read is of 2 bytes and the stride to reach the next element is 3 bytes:
- the first element read is
1
(0000 0000 0000 0001
), - the second element to be read is after skipping 3 bytes comes out to be
0
(0000 0000 | 0000 0000
), half are bytes from the number512
and the other half from the number0
- the last element to be read after another stride of 3 bytes is
3
:0000 0000 0000 0011
So, where am I going wrong? how is the middle element 2
in the strided output and not 0