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
2009-12-20 02:41:53
+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
2009-12-20 03:03:21