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

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

Christian Seberino
Guest
Posts: n/a

 04-20-2004
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

Russell E. Owen
Guest
Posts: n/a

 04-21-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
Guest
Posts: n/a

 04-22-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" <(E-Mail Removed)> wrote in message news:<c66jij\$nps\$(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (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

David M. Cooke
Guest
Posts: n/a

 04-23-2004
At some point, (E-Mail Removed) (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

Christian Seberino
Guest
Posts: n/a

 04-27-2004
David

Thanks. That was pretty clever methinks.

Chris

(E-Mail Removed) (David M. Cooke) wrote in message news:<(E-Mail Removed)>. ..
> At some point, (E-Mail Removed) (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.