mercredi 9 mai 2018

Apply different functions to an array depending on the value

I'm attempting to apply a different function to the first and second part of a numpy array. So in this example, multiplying the terms less than 0.5 by 2 and adding 1 to the terms of 0.5 and above.

x = numpy.linspace(0,1,11)
def test():
    for i in x:
        if i < 0.5:
            a = i*2
        else:
            a = i+1.0
    return(a)

print(test())

Which I want to return:

[0,0.2,0.4,0.6,0.8,1.5,1.6,1.7,1.8,1.9,2]

Thank You.

Aucun commentaire:

Enregistrer un commentaire