如何将较小的2D数组添加到较大的数组? [英] How can I add small 2D array to larger array?
本文介绍了如何将较小的2D数组添加到较大的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个较大的2D数组,我想添加一个较小的2D数组.
I have a larger 2D array, and I would like to add a smaller 2D array.
from numpy import *
x = range(25)
x = reshape(x,(5,5))
print x
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
y = [66,66,66,66]
y = reshape(y,(2,2))
print y
[[66 66]
[66 66]]
我想将数组 y
中的值添加到从 1,1
开始的 x
中,以便使 x
看起来像这样:
I would like to add the values from array y
to x
starting at 1,1
so that x
looks like this:
[[ 0 1 2 3 4]
[ 5 72 73 8 9]
[10 77 78 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
切片有可能吗?有人可以建议使用slice语句的正确格式来实现这一目标吗?
Is this possible with slicing? Can someone please suggest the correct formatting of the slice statement to achieve this?
谢谢
推荐答案
是的,您可以在numpy数组上使用切片:
Yes, you can use slicing on numpy arrays:
In [20]: x[1:3,1:3] += y
In [21]: print x
[[ 0 1 2 3 4]
[ 5 72 73 8 9]
[10 77 78 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
这篇关于如何将较小的2D数组添加到较大的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文