How to "embed" a small numpy array into a predefined block of a large numpy array?

Question:

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][:]
Asked By: nye17

||

Answers:

you can use multi dimension index:

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

the output is:

>>> 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]])
Answered By: HYRY

Here is a solution that works even if the position given goes off the edges of the wall numpy array, and works for any number of dimensions for your wall and block (note: the provided location loc needs to be a tuple, even in the 1D case).

def paste_slices(tup):
  pos, w, max_w = tup
  wall_min = max(pos, 0)
  wall_max = min(pos+w, max_w)
  block_min = -min(pos, 0)
  block_max = max_w-max(pos+w, max_w)
  block_max = block_max if block_max != 0 else None
  return slice(wall_min, wall_max), slice(block_min, block_max)

def paste(wall, block, loc):
  loc_zip = zip(loc, block.shape, wall.shape)
  wall_slices, block_slices = zip(*map(paste_slices, loc_zip))
  wall[wall_slices] = block[block_slices]

Tests:

1D

>>> b = np.zeros([10])
>>> a = np.arange(1, 5)
>>> b
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> a
array([1, 2, 3, 4])
>>> paste(b, a, (8,))
>>> b
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  2.])

2D

>>> b = np.zeros([10, 10])
>>> a = np.arange(1,33).reshape(4,8)
>>> b
array([[ 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.,  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.]])
>>> a
array([[ 1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16],
       [17, 18, 19, 20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29, 30, 31, 32]])
>>> paste(b, a, (-1, -3))
>>> b
array([[ 12.,  13.,  14.,  15.,  16.,   0.,   0.,   0.,   0.,   0.],
       [ 20.,  21.,  22.,  23.,  24.,   0.,   0.,   0.,   0.,   0.],
       [ 28.,  29.,  30.,  31.,  32.,   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.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]])
Answered By: Multihunter
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.