Numpy, adding a row to a matrix
robert.kern at gmail.com
Mon Jun 9 22:04:32 CEST 2008
> I have a numpy array (2 rows 3 colums)
> import numpy
> a=numpy.array( [ [1,2,3] , [3,3,1] ])
> I wish to add a row, this is how i do it
> a[s]=new row vector.
> Q: Is this a costly operation?
It can be if you have large arrays.
> What happens if i have to it several
> (and unknown) number of times?
> is there a simpler way to add a row?
Generally speaking, you shouldn't resize numpy arrays. If you need to construct
an array by appending, build up a list instead and use vstack() (or hstack() or
dstack() or column_stack() or concatenate() depending on the geometry).
We also have a numpy mailing list, which you should direct future numpy
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list