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.

Julien
  • 13,986
  • 5
  • 29
  • 53
Eman
  • 165
  • 1
  • 1
  • 8
  • 1
    `np.hstack` and `np.vstack` for example – Julien Nov 21 '16 at 03: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. – hpaulj Nov 21 '16 at 06:02

1 Answers1

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])
Nurjan
  • 5,889
  • 5
  • 34
  • 54