1

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?

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
  • Look into [numpy.concatenate](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.concatenate.html#numpy.concatenate). – Austin Jan 31 '19 at 14:59

1 Answers1

0

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.]])
Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72