I have a two dimensional array t
in numpy:
>>> t = numpy.array(range(81)).reshape((9,9)) >>> t array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8], [ 9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42, 43, 44], [45, 46, 47, 48, 49, 50, 51, 52, 53], [54, 55, 56, 57, 58, 59, 60, 61, 62], [63, 64, 65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78, 79, 80]])
It is indexed by two numbers: the row and column index.
>>> t[2,3] 21 >>> t.shape (9, 9) >>> t.strides (72, 8)
What I want to do is split the array into rectangular cells of a fixed size , for example 3 × 3. I would like to avoid copying the memory. The way I'm trying to do this is to create a view on t
with the appropriate form and steps ( (3,3,3,3)
and (216,24,72,8)
respectively). Thus, the first two view indices will indicate the position of a 3 × 3 cell in the larger grid, and the last two will indicate the position of an element within the cell. For example, t[0,1,:,:]
will return
array([[ 3, 4, 5], [12, 13, 14], [21, 22, 23]])
So my question is: how to create the described view? Am I missing a simpler method? Can this be done elegantly with slice syntax?