title: quickly calculate randomized 3D numpy array from 2D numpy array
body: <p>I have a 2-dimensional array of integers, we'll call it "A". </p>
  
  <p>I want to create a 3-dimensional array "B" of all 1s and 0s such that:</p>
  
  <ul>
  <li>for any fixed (i,j) <code>sum(B[i,j,:])==A[i.j]</code>, that is, <code>B[i,j,:]</code> contains <code>A[i,j]</code> <code>1s</code> in it </li>
  <li>the 1s are randomly placed in the 3rd dimension. </li>
  </ul>
  
  <p>I know how I would do this using standard python indexing but this turns out to be very slow. </p>
  
  <p>I am looking for a way to do this that takes advantage of the features that can make Numpy fast.</p>
  
  <p>Here is how I would do it using standard indexing:</p>
  
  <pre><code>B=np.zeros((X,Y,Z))
  indexoptions=range(Z)
  
  for i in xrange(Y):
      for j in xrange(X):
          replacedindices=np.random.choice(indexoptions,size=A[i,j],replace=False)
          B[i,j,[replacedindices]]=1
  </code></pre>
  
  <p>Can someone please explain how I can do this in a faster way?</p>
  
  <p>Edit: Here is an example "A":</p>
  
  <pre><code>A=np.array([[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]])
  </code></pre>
  
  <p>in this case X=Y=5 and Z>=5</p>