Home > Software design >  Stick the 3d numpy arrays and then remove the specific row
Stick the 3d numpy arrays and then remove the specific row

Time:03-12

I have two numpy arrays with size (1,5,2) and (1, 1,2). I want to stick two of them in one array and build one array with (1, 6,2) and then delete the first row of the matrix and final array should be with size of (1,5,2).

import numpy as np
a = np.random.rand(1, 5, 2)
b = np.random.rand(1, 1, 2)

For example,

a = array([[[0.3324251 , 0.23363885],
    [0.58601194, 0.56248613],
    [0.38604242, 0.29020216],
    [0.11876984, 0.6805301 ],
    [0.67216552, 0.43272049]]])
b = array([[[0.25492274, 0.43373311]]])

output:

a = array([[[0.58601194, 0.56248613],
    [0.38604242, 0.29020216],
    [0.11876984, 0.6805301 ],
    [0.67216552, 0.43272049],
    [0.25492274, 0.43373311]]])

CodePudding user response:

You can use the hstack and then delete the first row.

 x = np.hstack((a, b))[:, 1:,:]

 x = array([[[0.58601194, 0.56248613],
    [0.38604242, 0.29020216],
    [0.11876984, 0.6805301 ],
    [0.67216552, 0.43272049],
    [0.25492274, 0.43373311]]])

CodePudding user response:

Use stack:

np.hstack((a, b))  # h is for horizontal, if you want to put an array above another one, use np.vstack
  • Related