Conditional operations on numpy arrays

后端 未结 3 1723
深忆病人
深忆病人 2021-01-04 21:48

I\'m new to NumPy, and I\'ve encountered a problem with running some conditional statements on numpy arrays. Let\'s say I have 3 numpy arrays that look like this:

a:

3条回答
  •  温柔的废话
    2021-01-04 22:05

    You can use numpy.where:

    np.where((a > 3) & (b > 8), c + b*2, c)
    #array([[ 0, 18,  0,  0],
    #       [ 0,  0,  0,  0],
    #       [ 0,  0,  0,  0]])
    

    Or arithmetically:

    c + b*2 * ((a > 3) & (b > 8))
    #array([[ 0, 18,  0,  0],
    #       [ 0,  0,  0,  0],
    #       [ 0,  0,  0,  0]])
    

提交回复
热议问题