我有两个 NumPy 数组:
import numpy as np
m = 3
x = np.array([1, 0, 0, np.inf, 0, 0, 1, 1, 2, np.inf, np.inf, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.arange(x.shape[0]-m+1)
Run Code Online (Sandbox Code Playgroud)
假设有np.infin 的地方x,该索引位置被称为i。对于每个i,我想设置y[i-m+1:i+m] = np.inf. 所以,更换后,y应该是这样的:
array([0, np.inf, np.inf, np.inf, np.inf, np.inf, 6, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, 13, 14, 15, 16, 17])
Run Code Online (Sandbox Code Playgroud)
当 的值m增加或减少时,这也应该起作用。
小智 1
所以你基本上已经有了解决方案,
import numpy as np
m = 3
x = np.array([1, 0, 0, np.inf, 0, 0, 1, 1, 2, np.inf, np.inf, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.arange(x.shape[0]-m+1)
idx = np.where(x == np.inf)[0] # grabs all i's
for i in idx: # applies the operation you described at each i
y[(i-m+1):(i+m)] = np.inf # slightly changed indices
Run Code Online (Sandbox Code Playgroud)
我认为你唯一错误的是我认为它应该是从i-m+1到i+m。希望有帮助!
| 归档时间: |
|
| 查看次数: |
169 次 |
| 最近记录: |