I want to associate values into an array by index, without iterating. Picture the following, but for hundreds of thousands of values:
to_populate = [
[[0, 0], 1],
[[1, 1], 100],
[[2, 3], 29],
[[3, 2], 33],
]
mat = np.empty((4, 4))
mat[:] = np.nan
for idx, val in to_populate:
x, y = idx
mat[x, y] = val
# array([[ 1., nan, nan, nan],
# [ nan, 100., nan, nan],
# [ nan, nan, nan, 29.],
# [ nan, nan, 33., nan]])
I'm thinking of it as conceptually like the reverse of np.argwhere, but with specific values rather than a predicate condition.
mat = np.random.randn(3, 3)
# array([[ 0.89298522, 0.41059024, 0.32770948],
# [-0.91956498, -0.11774805, -1.42625182],
# [ 1.28644586, -0.06951971, -0.88742959]])
np.argwhere(mat < 0)
# array([[1, 0],
# [1, 1],
# [1, 2],
# [2, 1],
# [2, 2]])