Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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.]])
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.