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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Seberino

    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
  • Russell E. Owen

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

    In article <bf23f78f.04042 00803.4b065887@ posting.google. com>,
    seberino@spawar .navy.mil (Christian Seberino) wrote:
    [color=blue]
    >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?[/color]

    Try numarray.resize

    -- Russell

    Comment

    • Christian Seberino

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

      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:
      [color=blue][color=green][color=darkred]
      >>> a[/color][/color][/color]
      array([[ 0, 1, 2, 3, 4, 5],
      [ 6, 7, 8, 9, 10, 11],
      [12, 13, 14, 15, 16, 17]])
      [color=blue][color=green][color=darkred]
      >>> a.resize((4,6))
      >>> a[/color][/color][/color]
      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]])
      [color=blue][color=green][color=darkred]
      >>> a.resize((4,7))
      >>> a[/color][/color][/color]
      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:<c66jij$np s$1@nntp6.u.was hington.edu>...[color=blue]
      > In article <bf23f78f.04042 00803.4b065887@ posting.google. com>,
      > seberino@spawar .navy.mil (Christian Seberino) wrote:
      >[color=green]
      > >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?[/color]
      >
      > Try numarray.resize
      >
      > -- Russell[/color]

      Comment

      • David M. Cooke

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

        At some point, seberino@spawar .navy.mil (Christian Seberino) wrote:
        [color=blue]
        > 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*.[/color]

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

        --
        |>|\/|<
        /--------------------------------------------------------------------------\
        |David M. Cooke
        |cookedm(at)phy sics(dot)mcmast er(dot)ca

        Comment

        • Christian Seberino

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

          David

          Thanks. That was pretty clever methinks.

          Chris


          cookedm+news@ph ysics.mcmaster. ca (David M. Cooke) wrote in message news:<87oepifli 4.fsf@arbutus.p hysics.mcmaster .ca>...[color=blue]
          > At some point, seberino@spawar .navy.mil (Christian Seberino) wrote:
          >[color=green]
          > > 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*.[/color]
          >
          > If rows works, you could do transpose/resize/transpose.[/color]

          Comment

          Working...