numpy replace array elements with average of 2*2 blocks

Viewed 907

I have an m*n matrix in numpy. I want to divide my matrix into 2*2 blocks and then replace each element with average of the elements in its block. for example consider the following array:

[
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
]

I want to produce this matrix:

[
    [3.5, 3.5, 5.5, 5.5]
    [3.5, 3.5, 5.5, 5.5]
    [11.5, 11.5, 13.5, 13.5]
    [11.5, 11.5, 13.5, 13.5]
]

What is the most efficient way to do this? Should I use for loops?

1 Answers
Related