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][:]
You can append a NumPy array to another NumPy array by using the append() method. In this example, a NumPy array “a” is created and then another array called “b” is created. Then we used the append() method and passed the two arrays.
Use concatenate() to Join Two Arrays Use numpy. concatenate() to merge the content of two or multiple arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray.
append() is used to append values to the end of an array. It takes in the following arguments: arr : values are attached to a copy of this array.
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]])
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.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With