vendredi 1 septembre 2017

Extract specific ranges from numpy array

I have 3D numpy array, for example, like this:

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]]])

Is there a way to index it in such a way that I select, for example, top right corner of 2x2 elements in the first plane, and a center 2x2 elements subarray from the second plane? So that I could then zero out the elements 2,3,6,7,21,22,25,26? Basically I need to zero out a chunk of fixed size, but at different (random) locations for each plane of my array.

Something like this: a[:, x: x+2, y: y+2]

where x and y have different values for each first dimension of a.




Aucun commentaire:

Enregistrer un commentaire