title: How to re-index 3d numpy array
body: <p>Lets say we have a 3D array like:</p>
  
  <p><code>array = np.arange(8).reshape(2,2, 2)</code>
  <code>new_array = np.zeros((2, 2, 2))</code></p>
  
  <p>and lets assume we have some new random x,y,z indices for our array</p>
  
  <pre><code>x,y,z = np.meshgrid(array, array, array)
  </code></pre>
  
  <p>What is the fastest way to re-index our array?</p>
  
  <p>A simple solution given here:</p>
  
  <pre><code>for x in range(0, 3):
      for y in range(0, 3):
          for z in range(0, 3):
              new_x = x_coord[x,y,z]
              new_y = y_coord[x,y,z]
              new_z = z_coord[x,y,z]
  
              new_array[x,y,z] = array[new_x, new_y, new_z]
  </code></pre>
  
  <p>Is there a <code>one-liner</code> for this that I am not aware of?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Yes, there is... very easy:</p>
  
  <pre><code>vol = np.arange(8).reshape(2,2, 2)
  arr = np.arange(2)
  x,y,z = np.meshgrid(arr, arr, arr)
  
  
  print(vol)
  print(vol[y, x, z]) ### ---&gt; You have to swap the axes here tho. Does anyone know why?
  
  [[[0 1]
    [2 3]]
  
   [[4 5]
    [6 7]]]
  [[[0 1]
    [2 3]]
  
   [[4 5]
    [6 7]]]
  </code></pre>
  
  <p>Also, it is very slow. Any ideas how to improve the performance?</p>