471,595 Members | 990 Online

# Numpy, adding a row to a matrix

Hello,
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

s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=new row vector.

Q: Is this a costly operation? What happens if i have to it several
(and unknown) number of times?
is there a simpler way to add a row?

Thank you in advance
Saptarshi
Jun 27 '08 #1
2 23388
sapsi wrote:
Hello,
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

s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=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?
numpy.vstack([a, newrow])

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
questions to:

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"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

Jun 27 '08 #2
Hello,
Thank you. Yes, I will post to the numpy mailing list in future.
Regards
Saptarshi
On Jun 9, 4:04*pm, Robert Kern <robert.k...@gmail.comwrote:
sapsi wrote:
Hello,
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
s=a.shape
numpy.resize(a,s[0]+1,s[1])
a[s[0]]=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?

numpy.vstack([a, newrow])

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
questions to:

* *http://www.scipy.org/Mailing_Lists

--
Robert Kern

"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 thoughit had
* an underlying truth."
* *-- Umberto Eco
Jun 27 '08 #3

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 22 posts views Thread by J | last post: by 2 posts views Thread by Chris Smith | last post: by 18 posts views Thread by robert | last post: by 1 post views Thread by Jianzhong Liu | last post: by 5 posts views Thread by auditory | last post: by 1 post views Thread by Michael O'Brien | last post: by 3 posts views Thread by lancered | last post: by 2 posts views Thread by devnew | last post: by 1 post views Thread by dazzler | last post: by reply views Thread by leo001 | last post: by reply views Thread by leo001 | last post: by reply views Thread by Yacine Si Tayeb | last post: by reply views Thread by DANILIN | last post: by reply views Thread by HarrySto | last post: by reply views Thread by boston | last post: by reply views Thread by kushant negi | last post: by reply views Thread by Hightopo | last post: by reply views Thread by Swagelvo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.