python - combining 2D arrays to 3D arrays -
hello have 3 numpy arrays given below.
>>> print [[ 1. 0. 0.] [ 3. 0. 0.] [ 5. 2. 0.] [ 2. 0. 0.] [ 1. 2. 1.]] >>> print b [[ 5. 9. 9.] [ 37. 8. 9.] [ 49. 8. 3.] [ 3. 3. 1.] [ 4. 4. 5.]] >>> >>> print c [[ 0. 0. 0.] [ 0. 6. 0.] [ 1. 4. 6.] [ 6. 2. 0.] [ 0. 5. 4.]]
i combine them
[[[ 1. 0. 0.] [ 5. 9. 9.] [ 0. 0. 0.]] [[ 3. 0. 0.] [ 37. 8. 9.] [ 0. 6. 0.]] [[ 5. 2. 0.] [ 49. 8. 3.] [ 1. 4. 6.]] [[ 2. 0. 0.] [ 3. 3. 1.] [ 6. 2. 0.]] [[ 1. 2. 1.] [ 4. 4. 5.] [ 0. 5. 4.]]]
that take 1 row each array. tell me simple way it? tried hstack
, vstack
. not giving desired result.
thanks !
a solution using numpy dstack
:
>>> import numpy np >>> np.dstack((a,b,c)).swapaxes(1,2) array([[[ 1, 0, 0], [ 5, 9, 9], [ 0, 0, 0]], [[ 3, 0, 0], [37, 8, 9], [ 0, 6, 0]], [[ 5, 2, 0], [49, 8, 3], [ 1, 4, 6]], [[ 2, 0, 0], [ 3, 3, 1], [ 6, 2, 0]], [[ 1, 2, 1], [ 4, 4, 5], [ 0, 5, 4]]])
Comments
Post a Comment