numpy中多条件选择

有时候我们需要用到各种elif才能实现一些功能,还记得excel中有if函数可以无限嵌套实现每次的if选择。

于是numpy中的where函数也可以实现这样的功能。
下面我们用这个函数编写一个haar函数。

import numpy as np

def h_function(z, a, b):
    '''
    a is the window.
    b is the center of function.
    z is the height or altitude in this case.(numpy array or one value)
    '''

    logical_positive = np.logical_and(z>=b, z<=b+a/2)
    logical_negative = np.logical_and(z>= b - a/2, z

如上函数可以实现返回haar函数。

你可能感兴趣的:(numpy中多条件选择)