我正在尝试对numpy数组执行一些条件检查,我的代码现在不是很python。有人能建议更有效的方法来写下面的代码吗?在
eps = 1.0e-04 #Fill in gaps in the surfaces##################################### for i in range(nrows): for j in range(ncols): if (ibound[i,j] == 1 and fabs(h[i,j]-nodata) <= eps): h[i,j] = L1TopOld[i,j]
您只需继续矢量方式:
让我们看一个例子(我使用的是previous comment)中提出的一个例子:
^{pr2}$你的
您可以使用^{} 。首先,让我们制作一些玩具数据(顺便说一句,如果你自己做这部分的话会有帮助):
现在我们可以决定要修补哪些:
^{pr2}$并使用此命令告诉
np.where
我们要切换的位置:正如评论中指出的,这假设
ibound
是一个只由0和1组成的掩码。如果你真的只想改变ibound == 1
(例如,不是2)的情况,那也很简单:{在这里给出的答案是相同的,因为在cd3中}是相同的
如果要修改原始阵列:
根据您的条件创建逻辑掩码:
^{pr2}$使用布尔索引修改数组
相关问题 更多 >
编程相关推荐