Indices of fixed size sub-matrices of numpy array

后端 未结 3 941
长情又很酷
长情又很酷 2020-12-31 10:59

I am implementing an algorithm which requires me to look at non-overlapping consecutive submatrices within a (strictly two dimensional) numpy array. eg, for the 12 by 12

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 11:35

    You can use the one-liner:

    r = 3
    c = 3
    lenr = a.shape[0]/r
    lenc = a.shape[1]/c
    np.array([a[i*r:(i+1)*r,j*c:(j+1)*c] for (i,j) in np.ndindex(lenr,lenc)]).reshape(lenr,lenc,r,c)
    

提交回复
热议问题