473,545 Members | 1,638 Online

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

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
Jul 18 '05 #1
4 2683
In article <bf************ **************@ posting.google. com>,
se******@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
Jul 18 '05 #2
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@spam.invali d> wrote in message news:<c6******* ***@nntp6.u.was hington.edu>... In article <bf************ **************@ posting.google. com>,
se******@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

Jul 18 '05 #3
At some point, se******@spawar .navy.mil (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)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #4
David

Thanks. That was pretty clever methinks.

Chris
co**********@ph ysics.mcmaster. ca (David M. Cooke) wrote in message news:<87******* *****@arbutus.p hysics.mcmaster .ca>...
At some point, se******@spawar .navy.mil (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.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.