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

Discussion in 'Python' started by Christian Seberino, Apr 20, 2004.

1. ### Christian SeberinoGuest

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?

Chris

Christian Seberino, Apr 20, 2004

2. ### Russell E. OwenGuest

In article <>,
(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

Russell E. Owen, Apr 21, 2004

3. ### Christian SeberinoGuest

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" <> wrote in message news:<c66jij\$nps\$>...
> In article <>,
> (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

Christian Seberino, Apr 22, 2004
4. ### David M. CookeGuest

Re: Easiest way to *add a column* to a 2d matrix/array innumarray???

At some point, (Christian Seberino) wrote:

> 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*.

If rows works, you could do transpose/resize/transpose.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca

David M. Cooke, Apr 23, 2004
5. ### Christian SeberinoGuest

David

Thanks. That was pretty clever methinks.

Chris

(David M. Cooke) wrote in message news:<>...
> At some point, (Christian Seberino) wrote:
>
> > 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*.

>
> If rows works, you could do transpose/resize/transpose.

Christian Seberino, Apr 27, 2004