An array is present as:
a[[12,31,5], [5,32,1]]
I wish to add a row of 1's such that it becomes:
a[[1,1,1], [12,31,5], [5,32,1]]
How to do it?
An array is present as:
a[[12,31,5], [5,32,1]]
I wish to add a row of 1's such that it becomes:
a[[1,1,1], [12,31,5], [5,32,1]]
How to do it?
All you need is np.vstack:
a= np.array([[12,31,5], [5,32,1]])
np.vstack([np.ones(a.shape[1]),a])
array([[ 1., 1., 1.],
[12., 31., 5.],
[ 5., 32., 1.]])
A slightly more involved is np.r_:
np.r_[np.ones(a.shape[1]).reshape((1,-1)),a]
array([[ 1., 1., 1.],
[12., 31., 5.],
[ 5., 32., 1.]])