假设有个矩阵
[ 1, 2, 3, 4, 5, 6]
[ 4, 5, 6, 7, 8, 9]
[ 1, 2, 3, 4, 5, 6]
[ 4, 5, 6, 7, 8, 9]
想要区域性2x2平均回填回去
[3, 3, 5, 5, 7, 7]
[3, 3, 5, 5, 7, 7]
[3, 3, 5, 5, 7, 7]
[3, 3, 5, 5, 7, 7]
或是变成
[3, 5, 7]
[3, 5, 7]
我只想到用循环解决
感觉没发挥到 numpy 的威力
是否有用聪明的用法
谢谢!
import numpy as np
matrix1 = np.arange(0,200,1)[np.newaxis,:]
matrix1 = np.reshape(matrix1,(20,10))
matrix2 = np.zeros((20,10))
for i in range(0,20,2):
for j in range(0,10,2):
print(matrix1[i, j])
temp = [matrix1[i,j],matrix1[i+1,j],matrix1[i,j+1],matrix1[i+1,j+1]]
average = sum(temp)/len(temp)
matrix2[i, j] = average
matrix2[i+1, j] = average
matrix2[i, j+1] = average
matrix2[i+1, j+1] = average
print(matrix2)