怎么尽量简短地对矩阵的切片用另一个矩阵赋值

img[i:i+16,j:j+16] = sample_new
其中sample_new是16*16矩阵,对img其中一个方块赋值,直接用‘=’赋值会出现这种错误IndexError: too many indices for array,有没有比较简短的语言可以实现,而不是在循环里一个一个赋值

你好,你那样的思路完全没有问题啊,IndexError: too many indices for array 是IndexError:说明是你的切片出来的矩阵和sample_new的大小不一样,可以查看一下
sample_new的shape看看

这里尝试了一下,基本没有问题,你可以试试

 import numpy as np 

img0 = np.array(range(324))  
img = img0.reshape(18,18)
print('赋值前:\n',img)
sample_new = (np.array(range(256,0,-1))).reshape(16,16)
print('sample_new:',sample_new)
img[1:17,1:17] = sample_new            #赋值
print('赋值后:\n',img)