使用Numpy Array在每个像素时间序列的特定位置插入值



假设我有一个三维Numpy数组。它是一年的每日数据和地球的1度像素,得到的形状为(365,180,360)。

现在,您要在其位置插入1月16日的值,以便每个时间序列变为:

…Val_0114, val_0115, val_0116, val_0116, val_0117, val_0118,…

我可以这样做:

arr_new = np.empty((arr_old.shape[0], 180, 360)) * np.nan
for _lat in range(arr_new.shape[1]):
for _lon in range(arr_new.shape[2]):
arr_new[:, _lat, _lon] = np.insert(arr_old, 15, arr_old[15, _lat, _lon], axis=0)

但是我想找到一种没有循环的更花哨的方法。

在numpy中,您可以直接引用矩阵。这类似于处理多维列表:

# If you want to place the same temperature in the whole world/pixels
arr_old[16] = 15
# If you want to place the same temperature in a certain place the whole year
arr_old[:][lat][lon]

最新更新