title: Numpy 1D-array operation in a ND-array
body: <p>Let's say we have a 4D-array A of shape (n, m, g, h) and a 1D-array B of shape (n).</p>
  
  <p>So I want to do a power operation for every 3D sub-array of A (m, g, h) with every element of B (which is an int).</p>
  
  <pre><code>A = np.arange(24).reshape(3, 2, 2, 2)
  &gt;&gt;&gt;array([[[[ 0,  1],
           [ 2,  3]],
  
          [[ 4,  5],
           [ 6,  7]]],
  
  
         [[[ 8,  9],
           [10, 11]],
  
          [[12, 13],
           [14, 15]]],
  
  
         [[[16, 17],
           [18, 19]],
  
          [[20, 21],
           [22, 23]]]]
  B = np.arange(3)
  &gt;&gt;&gt;array([0, 1, 2])
  </code></pre>
  
  <p>The result that I want is:</p>
  
  <pre><code>C = somefunc(A, B) # just an example, can be anything
  &gt;&gt;&gt;array([[[[ (ignore, 0^0),  1],
           [ 1,  1]],
  
          [[ 1,  1],
           [ 1,  1]]],
  
  
         [[[ 8,  9],
           [10, 11]],
  
          [[12, 13],
           [14, 15]]],
  
  
         [[[256, 289],
           [324, 361]],
  
          [[400, 441],
           [484, 529]]]]
  </code></pre>
  
  <p>What is the best way to get the desired result?</p>
  
  <p>I thought about something like:</p>
  
  <pre><code>A = [np.power(A[i, :, :, :], B[i]) for i in range(B.size)]
  </code></pre>
  
  <p>But that would be quite inefficient if B.size is a big number. Any other ideas?</p>