[Numpy-discussion] Beginner's question

Brett Olsen brett.olsen at gmail.com
Wed Apr 20 11:03:36 EDT 2011


On Sat, Apr 16, 2011 at 2:08 PM, Laszlo Nagy <gandalf at shopzeus.com> wrote:
> 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
> N = 10
> X = rnd.randint( 1,25, (4,N))
> print dim_weight(X)
>    Laszlo

This works:

def dim_weight2(X):
    w = X[0]
    v = X[1]*X[2]*X[3]
    res = np.empty(len(volumes), dtype=np.double)
    res[:] = w[:]
    res[v>5184] = v[v>5184]/194.0
    return res

~Brett



More information about the NumPy-Discussion mailing list