0

I would like to know how I can add row to a matrix in python numpy. For instance, in Matlab we can do A=[1;zeros(10,1)], how can I do that in numpy?

Thanks.

2
  • 1
    np.hstack and np.vstack for example Commented Nov 21, 2016 at 3:00
  • That MATLAB syntax is equivalent to its cat, horzcat and vertcat functions. In numpy concatenate is the basic function, hstack and vstack use it. Make sure you understand dimensions (especially the fact that np.array may have 0 or 1 dim, not just 2. Commented Nov 21, 2016 at 6:02

1 Answer 1

3

Try this:

import numpy

...

# L is your 2-dimensional list

M = numpy.matrix(L)
# R is the list (i.e. a row) to add
M = numpy.vstack([M, R])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.