tags:

views:

63

answers:

2

Suppose I have three "sheets" of matrix a,b and c, each with the same m*n*p dimension. And I want to combine them to get a new m*n*p*3 matrix whose (i,j,k) element is (a[i,j,k],b[i,j,k],c[i,j,k]). Which command should I use ? The dstack command seems not work here. Thanks.

+2  A: 

I think what you want is:

np.concatenate([np.expand_dims(x, -1) for x in (a, b, c)], axis=3)
Alex Martelli
+3  A: 

Another one liner would be:

result = numpy.array( (a,b,c) ).transpose( (1,2,3,0) )

or a more self-descriptive method:

result = empty( (m,n,p,3) )
result[:,:,:,0] = a
result[:,:,:,1] = b
result[:,:,:,2] = c
Vincent Marchetti