I have the following nested for loop (randoms for simplicity):
import numpy as np
lat_idx = np.random.randint(121, size = 4800)
lon_idx = np.random.randint(201, size = (4800,4800))
sum_cell = np.zeros((121,201))
data = np.random.rand(4800,4800)
for j in range(4800):
for i in range(4800):
if lat_idx[i] < 0 or lon_idx[i, j] < 0:
continue
sum_cell[lat_idx[i], lon_idx[i, j]] += data[i, j]
#print(sum_cell)
Is there a way to write it as matrix operation or with some "numpy action"? At the moment it is really slow. My problem is that lon_idx is both dependent on i and j.
np.add.atfunction, since you'll be combining multipled[i,j]elements into onesum_cell.lat_idx[i] < 0 or lon_idx[i, j] < 0be true, if you are using only positive numbers?