[Numpy-discussion] NumPy-Discussion Digest, Vol 55, Issue 42

Ken Basye kbasye1 at jhu.edu
Sun Apr 17 14:01:11 EDT 2011


I think this does what you want:

def dim_weight(x):
     weights = x[0]
     volumes = x[1]*x[2]*x[3]
     return np.where(volumes>5184, volumes / 194.0, weights)

Best,
     Ken


On 4/17/11 1:00 PM, Laszlo Nagy <gandalf at shopzeus.com> wrote:
> Message: 1
> Date: Sat, 16 Apr 2011 21:08:55 +0200
> From: Laszlo Nagy<gandalf at shopzeus.com>
> Subject: [Numpy-discussion] Beginner's question
> To: Discussion of Numerical Python<numpy-discussion at scipy.org>
> Message-ID:<4DA9E947.7060209 at shopzeus.com>
> Content-Type: text/plain; charset=ISO-8859-2; format=flowed
>
>
>     Hi All,
>
> I have this example program:
>
> import numpy as np
> import numpy.random as rnd
>
> def dim_weight(X):
>       weights = X[0]
>       volumes = X[1]*X[2]*X[3]
>       res = np.empty(len(volumes), dtype=np.double)
>       for i,v in enumerate(volumes):
>           if v>5184:
>               res[i] = v/194.0
>           else:
>               res[i] = weights[i]
>       return res
>
>
> # TEST
> N = 10
> X = rnd.randint( 1,25, (4,N))
> print dim_weight(X)
>
> I want to implement the dim_weight() function effectively. But I'm not
> sure how to construct a numpy expression that does the same think for me.
>
> I got to this point:
>
> def dim_weight(X):
>       weights = X[0]
>       volumes = X[1]*X[2]*X[3]
>       dweights = volumes/194.0
>       return ( weights[weights>5184] , dweights[weights<=5184] ) # ???
>
> I don't know how to preserve the order of the elements and return the
> result in one array. Maybe I need to do create a matrix multiply matrix?
> But don't know how.
>
> Thanks,
>
>      Laszlo
>



More information about the NumPy-Discussion mailing list