如何“嵌入"一个小的numpy数组变成一个大的numpy数组的预定义块? [英] How to "embed" a small numpy array into a predefined block of a large numpy array?

查看:137
本文介绍了如何“嵌入"一个小的numpy数组变成一个大的numpy数组的预定义块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的NXN阵列块",我想将其插入大阵列墙"的指定区域(即开始"处的对角线区域).有一种有效的方法可以对此进行存档吗?

I have a small NXN array "block" that I want to plug into a specified region (i.e., a diagonal region at "start") of a large array "wall". Is there an efficient method to archive this?

wall[start:start+N][start:start+N] = block[:][:]

目前我正在做的很简单:

currently what I am doing is simply:

for i in xrange(N):
    wall[start+i][start:start+N] = block[i][:]

推荐答案

您可以使用多维索引:

import numpy as np

wall = np.zeros((10,10),dtype=np.int)
block = np.arange(1,7).reshape(2,3)

x = 2
y = 3
wall[x:x+block.shape[0], y:y+block.shape[1]] = block

输出为:

>>> wall
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
       [0, 0, 0, 4, 5, 6, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

这篇关于如何“嵌入"一个小的numpy数组变成一个大的numpy数组的预定义块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆