2021年3月14日星期日

Numpy: How to slice or split 2D subsections of 2D array

I have a 2D array. For example:

ary = np.arange(24).reshape(6,4)    [[ 0  1  2  3]   [ 4  5  6  7]   [ 8  9 10 11]   [12 13 14 15]   [16 17 18 19]   [20 21 22 23]]  

I want to break this into smaller 2D arrays, each 2x2, and compute the square root of the sum of each. I actually want to use arbitrary sized sub-arrays, and compute arbitrary functions of them, but I think this question is easier to ask with concrete operations and concrete array sizes, so in this example starting with a 6x4 array and computing the square root of sums of 2x2 sub-arrays, the final result would be a 3x2 array, as follows:

[[3.16, 4.24]    # math.sqrt(0+1+4+5)    , math.sqrt(2+3+6+7)   [6.48, 7.07]    # math.sqrt(8+9+12+13)  , math.sqrt(10+11+14+15)   [8.60, 9.05]]   # math.sqrt(16+17+20+21), math.sqrt(18+19+22+23)  

How can I slice, or split, or do some operation to perform some computation on 2D sub-arrays?

https://stackoverflow.com/questions/66631871/numpy-how-to-slice-or-split-2d-subsections-of-2d-array March 15, 2021 at 11:06AM

没有评论:

发表评论