[SciPy-User] re[SciPy-user] moving for loops...

Benjamin Root ben.root@ou....
Thu Jun 17 11:33:27 CDT 2010

Well, there is numpy.tile() that can replicate a numpy array multiple times
in various dimensions, but I have to wonder if you need to step back and
think about why your dimensions aren't matching up.  If you are pulling in
data where one of the dimensions has a length of 90, but the corresponding
array that denotes that dimension (timesteps) has a length of 30, then I
have to wonder if there needs to be some rethinking of the overall design.

Ben Root

On Thu, Jun 17, 2010 at 11:02 AM, mdekauwe <mdekauwe@gmail.com> wrote:

> So what happens if I need to extend in two directions at once? So for
> example
> I have 2 arrays...
> timesteps = np.arange(30)
> and
> y which has dimensions 90, 3, where each row has values of say 1.2, 3.4,
> 5.5,
> then I have some function I call "func", where the arguments are timesteps
> and each of the rows of y, e.g.
> func(x, y[0,:])
> However if I wanted to carry out this step for each of the rows (90) of y,
> I
> can't seem to broadcast this correctly.
> tmp = func(timesteps[:,np.newaxis], y)
> I can see why, as this will only stretch timesteps in one direction so that
> it becomes (30, 3), so is there a nice way to stretch the rows as well? I
> thought perhaps I needed to reshape timesteps first, but I didn't seem to
> solve it that way either.
> Incase none of this made sense... my original loop version
> tmp = np.zeros((90, 30))
> for i in xrange(len(y)):
>    tmp[i,:] = func(timsteps, y[i])
> I have the feeling I am missing something very obvious here! Thanks!
> --
> View this message in context:
> http://old.nabble.com/removing-for-loops...-tp28633477p28916343.html
> Sent from the Scipy-User mailing list archive at Nabble.com.
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100617/4f9f7c6b/attachment.html 

More information about the SciPy-User mailing list