You can first sort the x_val
array in acsending order and then assign to im
. If an index occurs multiple times the last assignment, which is the highest value, will set the final value.
import numpy as np
im = np.zeros((10,10))
idx = np.array(
[[3, 3], [7, 3],[0, 1],[0, 6],[6, 9],[8, 1],[7, 3],[8, 3],[8, 4],[9, 5]]
)
x_val = np.array([
[0.17166161],
[0.80913063],
[0.52597124],
[0.27078974],
[0.35230144],
[0.66411425],
[0.28035714],
[0.76413514],
[0.27064702],
[0.54131715]]
)
order = x_val.argsort(0)[:,0]
im[idx[order][:, 1], idx[order][:, 0]] = x_val[order][:,0]
Result:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0.53 0. 0. 0. 0. 0. 0. 0. 0.66 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0.17 0. 0. 0. 0.81 0.76 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0.27 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.54]
[0.27 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0.35 0. 0. 0. ]]
(I used the indexing order from your OP, i.e. idx[1]
, which is [7,3]
, will go into im[3,7]
so that im[3, 7] == 0.80913063
)