I have a matrix like below
c = [[ 1 2 3 4 5 6 7 8 9 1]
[ 2 3 4 5 6 7 8 9 1 2]
[ 3 4 5 6 7 8 9 1 2 3]
[ 4 5 6 7 8 9 1 2 3 4]]
From the given SO ANSWERS in this post, I used it to divide the matrix into blocks (2*5) like below
def blockshaped(arr, nrows, ncols):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
h, w = arr.shape
assert h % nrows == 0, "{} rows is not evenly divisble by {}".format(h, nrows)
assert w % ncols == 0, "{} cols is not evenly divisble by {}".format(w, ncols)
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
print(blockshaped(c, 2, 5))
Result:
[[[ 1 2 3 4 5 ]
[ 2 3 4 5 6 ]]
[[ 6 7 8 9 1 ]
[ 7 8 9 1 2]]
[[ 3 4 5 6 7 ]
[ 4 5 6 7 8 ]]
[[ 8 9 1 2 3 ]
[ 9 1 2 3 4]]]
I got 4 blocks of matrix, and now I need the mean value of each block. How to calculate the mean of this each block?
When I try to use the mean() it is going to calculate the mean for the whole matrix but not for each block.