Easiest way to *add a column* to a 2d matrix/array in numarray???

Christian Seberino seberino at spawar.navy.mil
Thu Apr 22 18:34:04 EDT 2004


Thanks for the reply.  I appreciate all the help I can get.  Your suggestion
of using resize is excellent for adding *rows* but does not seem
right for *columns*.  Here is an example:

>>> a
array([[ 0,  1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10, 11],
           [12, 13, 14, 15, 16, 17]])

>>> a.resize((4,6))
>>> a
array([[ 0,  1,  2,  3,  4,  5],
          [ 6,  7,  8,  9, 10, 11],
          [12, 13, 14, 15, 16, 17],
          [ 0,  1,  2,  3,  4,  5]])

>>> a.resize((4,7))
>>> a
array([[ 0,  1,  2,  3,  4,  5,  6],
          [ 7,  8,  9, 10, 11, 12, 13],
          [14, 15, 16, 17,  0,  1,  2],
          [ 3,  4,  5,  0,  1,  2,  3]])

Do you see how adding an extra row left old rows intact but
adding an extra column messes up old columns? (i.e.
data in (1,1) position is not the same after adding a column)

Chris


"Russell E. Owen" <no at spam.invalid> wrote in message news:<c66jij$nps$1 at nntp6.u.washington.edu>...
> In article <bf23f78f.0404200803.4b065887 at posting.google.com>,
>  seberino at spawar.navy.mil (Christian Seberino) wrote:
> 
> >How add a column to a 2d array/matrix in numarray???
> >
> >The unelegant way I found was to:
> >
> >1. Create a new array with an extra column (e.g. using 'zeros' function).
> >2. Copy original array into new array.
> >3. Copy new column into last column.
> >
> >Is there a slicker way to do this?
> 
> Try numarray.resize
> 
> -- Russell



More information about the Python-list mailing list