I have a two-dimensional array of integers, we will call it "A".
I want to create a three-dimensional array "B" of all 1s and 0s so that:
- for any fixed (i, j)
sum(B[i,j,:])==A[ij] B[i,j,:] sum(B[i,j,:])==A[ij] , that is, B[i,j,:] contains A[i,j] 1s in it - 1s are placed randomly in the 3rd dimension.
I know how to do this using standard python indexing, but this proves to be very slow.
I'm looking for a way to do this, taking advantage of features that Numpy can quickly do.
Here's how I would do it using standard indexing:
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
Can someone explain how I can do this faster?
Edit: Here is an example of "A":
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]])
in this case X = Y = 5 and Z> = 5