简体   繁体   English

用另一个数组中的值替换numpy数组的切片

[英]Replace slice of a numpy array with values from another array

Say I've got two numpy arrays which were created this way: 假设我有两个以这种方式创建的numpy数组:

zeros = np.zeros((270,270))
ones = np.ones((150,150))

How can I insert ones in zeros at position [60,60] ? 如何在位置[60,60] zeros位置插入ones I want an array that looks like a "square in the square". 我想要一个看起来像“正方形中的正方形”的数组。

I've tried the following two options: 我尝试了以下两种选择:

np.put(empty, [60,60], ones)
np.put(empty, [3541], ones)
np.put[empty, [60:210,60:210], ones)

but the latter yields invalid syntax and the first two don't work either. 但后者会产生无效的语法,而前两个语法也不起作用。 Has anyone got an idea how this could work? 有谁知道这如何工作?

这是将零值替换为1的一种方法。

zeros[60:210,60:210] = ones

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM