Hello I have 3 numpy arrays as given below.
>>> print A
[[ 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 would like to combine them as
[[[  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 is I would like to take one row from each array.
Could anyone tell me a simple way to do it?
I already tried hstack, vstack. But they are not giving the desired result.
Thanks !
Using np.stack makes this trivial:
>>> np.stack([A, B, C], axis=1)  # stack along a new axis in axis 1 of the result
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]]])
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With