Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-使用numpy函数(如numpy.where)替换多个if-elif语句和条件_Python_Numpy_If Statement - Fatal编程技术网

python-使用numpy函数(如numpy.where)替换多个if-elif语句和条件

python-使用numpy函数(如numpy.where)替换多个if-elif语句和条件,python,numpy,if-statement,Python,Numpy,If Statement,Radio_index、n_x和n_y是整数 我编写了一个可以运行的if/elif代码。 目的是为无线电索引查找x,y位置 我能用np吗 def radio_index2xy(radio_index,n_x,n_y): con1 = radio_index <= n_x con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1) con3 = (radio_index > n_x+n_

Radio_index、n_x和n_y是整数 我编写了一个可以运行的if/elif代码。 目的是为无线电索引查找x,y位置 我能用np吗

def radio_index2xy(radio_index,n_x,n_y): 

    con1 = radio_index <= n_x
    con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
    con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
    con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
    condlist = [[con1],[con2],[con3],[con4]]
    choicelist = [[x_pos = radio_index -1 ,y_pos = 0],\
                  [(x_pos = n_x -1),(y_pos = radio_index - n_x)],\
                  [(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],\
                  [(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
    np.select(condlist,choicelist)

    return x_pos,y_pos 





if radio_index <= n_x:
    x_pos = radio_index -1
    y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
    x_pos = n_x -1
    y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
    x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
    y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
    x_pos = 0
    y_pos = 2*n_x+2*n_y-4-radio_index+1
def radio_indexxy(radio_索引,n_x,n_y):

con1=radio_index n_x)和(radio_index n_x+n_y-1)和(radio_index 2*n_x+n_y-2)和(radio_index
np。searchsorted
对这种逻辑很有用:

def radio_index2xy_v(radio_index, n_x, n_y):
    sgn = np.array([0, 1, 1, -1, -1, 0])
    col = np.array([-1, 1, 0, 1, 0, -1])
    coeffs = np.array([[-1, -1],
                       [0, -1],
                       [-n_x, n_x - 1],
                       [n_y - 1, 2*n_x + n_y - 2],
                       [2*n_x + 2*n_y - 3, 0],
                       [-1, -1]])
    cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
    idx = cusps.searchsorted(radio_index)
    out = coeffs[idx]
    out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
    return out
演示:


请告诉我们代码应该做什么,我不认为任何人会对其进行反向工程。此外,该代码中没有一个
if
/
elif
。它用于从给定的无线电索引中查找x(垂直轴)和y(水平轴)。可以运行if/elif方法。
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
       [ 0,  0],
       [ 0,  1],
       [ 0,  2],
       [ 0,  3],
       [ 0,  4],
       [ 1,  4],
       [ 2,  4],
       [ 3,  4],
       [ 3,  3],
       [ 3,  2],
       [ 3,  1],
       [ 3,  0],
       [ 2,  0],
       [ 1,  0],
       [-1, -1],
       [-1, -1],
       [-1, -1],
       [-1, -1],
       [-1, -1]])