I don't know if the title is apprpriate or not, but let me show you what I want to do,
In [56]: import numpy as np
In [57]: a= np.random.rand(2,2,2); a
Out[57]:
array([[[0.4300565 , 0.82251319],
[0.56113378, 0.83284255]],
[[0.00822414, 0.28256243],
[0.16648411, 0.33381438]]])
In [58]: b=np.random.rand(2); b
Out[58]: array([0.8035224 , 0.09884653])
In [59]: np.stack(( np.column_stack((b,a[:,i,:])) for i in range(a.shape[1])))
Out[59]:
array([[[0.8035224 , 0.4300565 , 0.82251319],
[0.09884653, 0.00822414, 0.28256243]],
[[0.8035224 , 0.56113378, 0.83284255],
[0.09884653, 0.16648411, 0.33381438]]])
So, I want to stack an array as column to an inner axis. Is it possible to do the looping structure more efficiently and concisely in numpy? I tried with numpy insert but could not do it.
another example
In [110]: a= np.random.rand(5,3,3); a
Out[110]:
array([[[0.27506756, 0.82334411, 0.7004287 ],
[0.6834928 , 0.28457133, 0.6275462 ],
[0.49744358, 0.25131814, 0.56422852]],
[[0.82591597, 0.92367306, 0.04652992],
[0.98545051, 0.92813944, 0.14360307],
[0.85454081, 0.8254149 , 0.5637401 ]],
[[0.59545519, 0.41563571, 0.41937218],
[0.90980491, 0.30169504, 0.96630809],
[0.06713389, 0.64357544, 0.12901734]],
[[0.47566444, 0.33476802, 0.26635363],
[0.4678913 , 0.53028241, 0.03112231],
[0.68445959, 0.07113376, 0.86651669]],
[[0.66951982, 0.01827502, 0.43831829],
[0.02798567, 0.36880876, 0.55029074],
[0.40127051, 0.6311474 , 0.51015882]]])
In [111]: b= np.random.rand(5,2); b
Out[111]:
array([[0.01659589, 0.15320541],
[0.79025065, 0.28041334],
[0.56024173, 0.49317082],
[0.28229119, 0.46010724],
[0.72239851, 0.62075004]])
In [112]: np.stack(( np.column_stack((b,a[:,i,:])) for i in range(a.shape[1])))
Out[112]:
array([[[0.01659589, 0.15320541, 0.27506756, 0.82334411, 0.7004287 ],
[0.79025065, 0.28041334, 0.82591597, 0.92367306, 0.04652992],
[0.56024173, 0.49317082, 0.59545519, 0.41563571, 0.41937218],
[0.28229119, 0.46010724, 0.47566444, 0.33476802, 0.26635363],
[0.72239851, 0.62075004, 0.66951982, 0.01827502, 0.43831829]],
[[0.01659589, 0.15320541, 0.6834928 , 0.28457133, 0.6275462 ],
[0.79025065, 0.28041334, 0.98545051, 0.92813944, 0.14360307],
[0.56024173, 0.49317082, 0.90980491, 0.30169504, 0.96630809],
[0.28229119, 0.46010724, 0.4678913 , 0.53028241, 0.03112231],
[0.72239851, 0.62075004, 0.02798567, 0.36880876, 0.55029074]],
[[0.01659589, 0.15320541, 0.49744358, 0.25131814, 0.56422852],
[0.79025065, 0.28041334, 0.85454081, 0.8254149 , 0.5637401 ],
[0.56024173, 0.49317082, 0.06713389, 0.64357544, 0.12901734],
[0.28229119, 0.46010724, 0.68445959, 0.07113376, 0.86651669],
[0.72239851, 0.62075004, 0.40127051, 0.6311474 , 0.51015882]]])
0.56113378is below0.4300565in arrayabut in the output is not.