title: Slicing different rows of a numpy array differently
body: <p>I'm working on a Monte Carlo radiative transfer code, which simulates firing photons through a medium and statistically modelling their random walk. It runs slowly firing one photon at a time, so I'd like to vectorize it and run perhaps 1000 photons at once.</p>
  
  <p>I have divided my slab through which the photons are passing into <code>nlayers</code> slices between optical depth <code>0</code> and <code>depth</code>. Effectively, that means that I have <code>nlayers + 2</code> regions (<code>nlayers</code> plus the region above the slab and the region below the slab). At each step, I have to keep track of which layers each photon passes through. </p>
  
  <p>Let's suppose that I already know that two photons start in layer 0. One takes a step and ends up in layer 2, and the other takes a step and ends up in layer 6. This is represented by an array <code>pastpresent</code> that looks like this:</p>
  
  <pre><code>[[ 0 2]
   [ 0 6]]
  </code></pre>
  
  <p>I want to generate an array <code>traveled_through</code> with <code>(nlayers + 2)</code> columns and 2 rows, describing whether photon <code>i</code> passed through layer <code>j</code> (endpoint-inclusive). It would look something like this (with <code>nlayers = 10</code>):</p>
  
  <pre><code>[[ 1 1 1 0 0 0 0 0 0 0 0 0]
   [ 1 1 1 1 1 1 1 0 0 0 0 0]]
  </code></pre>
  
  <p>I could do this by iterating over the photons and generating each row of <code>traveled_through</code> individually, but that's rather slow, and sort of defeats the point of running many photons at once, so I'd rather not do that.</p>
  
  <p>I tried to define the array as follows:</p>
  
  <pre><code>traveled_through = np.zeros((2, nlayers)).astype(int)
  traveled_through[ : , np.min(pastpresent, axis = 1) : np.max(pastpresent, axis = 1) + ] = 1
  </code></pre>
  
  <p>The idea was that in a given photon's row, the indices from the starting layer through and including the ending layer would be set to 1, with all others remaining 0. However, I get the following error:</p>
  
  <pre><code>traveled_through[ : , np.min(pastpresent, axis = 1) : np.max(pastpresent, axis = 1) + 1 ] = 1
  IndexError: invalid slice
  </code></pre>
  
  <p>My best guess is that numpy does not allow different rows of an array to be indexed differently using this method. Does anyone have suggestions for how to generate <code>traveled_through</code> for an arbitrary number of photons and an arbitrary number of layers?</p>