[Numpy-discussion] building an array using smaller arrays

Rudolf Sykora rudolf.sykora at gmail.com
Sun Mar 4 11:31:10 EST 2007


Thank you for your reply very much indeed. :)
I guessed it could somehow so..., though I thought it could be somwhat
simpler (not saying that your solution is too complicated :))
Ruda

On 01/03/07, David M. Cooke <cookedm at physics.mcmaster.ca> wrote:
>
> On Mar 1, 2007, at 13:33 , Rudolf Sykora wrote:
>
> > Hello,
> >
> > since noone has reacted to my last e-mail yet (for several days), I
> > feel the need to ask again (since I still do not know a good answer).
> > Please help me.
> >
> > >> Hello everybody,
> > >> I wonder how I could most easily accomplish the following:
> > >>
> > >>Say I have sth like:
> > >> a = array( [1, 2] )
> > >> and I want to use this array to build another array in the
> > following sence:
> > >> b = array( [[1, 2, 3, a], [5, a, 6, 7], [0, 2-a, 3, 4]])  # this
> > doesn't work
> > >>
> > >> I would like to obtain
> > >> b = array( [[1, 2, 3, 1, 2],  [5 ,1 ,2 ,6 ,7], [0, 1, 0, 3, 4]] )
> >
> > >> I know a rather complicated way but believe there must be an
> > easy one.
> > >> Thank you very much.
> >
> > >> Ruda
> >
> > I would need some sort of flattening operator...
> > The solution I know is very ugly:
> >
> >  b = array(( concatenate(([1, 2, 3], a)), concatenate(([5], a, [6,
> > 7])), concatenate(([0], 2-a, [3, 4])) ))
>
> Define a helper function
>
> def row(*args):
>      res = []
>      for a in args:
>          a = asarray(a)
>          if len(a.shape) == 0:
>              res.append(a)
>          elif len(a.shape) == 1:
>              res += a.tolist()
>          else:
>              raise ValueError("arguments to row must be row-like")
>      return array(res)
>
> then
>
> b = array([ row(1,2,3,a), row(5,a,6,7), row(0,2-a,3,4) ])
>
> --
> |>|\/|<
> /------------------------------------------------------------------\
> |David M. Cooke              http://arbutus.physics.mcmaster.ca/dmc/
> |cookedm at physics.mcmaster.ca
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070304/fd2bbf53/attachment.html>


More information about the NumPy-Discussion mailing list