title: How to add different arrays from the center point of an array in Python/NumPy
body: <p>following on from " <a href="https://stackoverflow.com/questions/9886303/">Adding different sized/shaped displaced NumPy matrices</a> ", I want to extend the code, so that I can do the addition at the center</p>
  
  <pre><code>import numpy as np
  
  #two 3d arrays, of different size.
  
  # b1 is variable in size, for the purposes of illustration, lets make it 5x5x5
  b1 = np.zeros((5,5,5), dtype=np.int)
  
  # b2 is a fixed size matrix in practice of 10x10x10, but for purposes of illustration,
  # its 3x3x3
  b2 = np.ones((3,3,3), dtype=np.int)
  
  # want to add b1 to b2, but at a specific location.
  # Here, for the purposes of illustration I use the location 2x2x2 in b1 so that the      added values fit in the
  # lower back corner of b1.
  
  pos_v, pos_h, pos_z = 2, 2, 2  # the 3d coordinates. I want b2 to be centered at 3,3,3 in b1, so this is a bit of a hack to make it fit
  
  v_range1 = slice(max(0, pos_v), max(min(pos_v + b2.shape[0], b1.shape[0]), 0))
  h_range1 = slice(max(0, pos_h), max(min(pos_h + b2.shape[1], b1.shape[1]), 0))
  z_range1 = slice(max(0, pos_z), max(min(pos_z + b2.shape[2], b1.shape[2]), 0))
  
  v_range2 = slice(max(0, -pos_v), min(-pos_v + b1.shape[0], b2.shape[0]))
  h_range2 = slice(max(0, -pos_h), min(-pos_h + b1.shape[1], b2.shape[1]))
  z_range2 = slice(max(0, -pos_z), min(-pos_z + b1.shape[2], b2.shape[2]))
  
  b1[v_range1, h_range1, z_range1] += b2[v_range2, h_range2, z_range2]
  </code></pre>
  
  <p>this gives a result for b1 as</p>
  
  <pre><code>&gt;&gt; b1
  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, 1, 1, 1],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1]],
  
     [[0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1]],
  
     [[0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1]]])
  </code></pre>
  
  <p>but ideally, I'd like to do the addition so that the center of b2 (the 3x3x3 matrix) is placed at the center of the actual target 3,3,3. Rather than the corner of the matrix at 2,2,2. The result in this example will be the same (because I artificially moved the target which is 3,3,3 to 2,2,2 so that it will hit the same position), but on the actual code I use it on (where the data within the matrix is not aligned to the matrix axes, it is oblique), the result would be quite different if I did a similar hack.</p>
  
  <p>In practice, my "b1" matrix will be of variable size, but much larger than 5x5x5, and "b2" will be 10x10x10 or larger. I have the precise location coordinates in my target matrix (b1) which is the center of where I want the center of data from b2 to be stamped (basically I'm working on 3d volume image data, and I want to put a sphere at a coordinate in a 3d volume). As I said, the data within the matrix is not aligned to the matrix axes, so modifying the target coordinate position by offsetting it for the size of b2 isn't the greatest of ideas (but one I considered). This might be being pedantic, but I would prefer getting it right from the start.</p>
  
  <p>Can anyone offer any advice?</p>