Given a source array
src = np.random.rand(320,240)
and an index array
idx = np.indices(src.shape).reshape(2, -1)
np.random.shuffle(idx.T)
we can map the linear index i
in src
to the 2-dimensional index idx[:,i]
in a destination array dst
via
dst = np.empty_like(src)
dst[tuple(idx)] = src.ravel()
This is discussed in Python: Mapping between two arrays with an index array
However, if this mapping is not 1-to-1, i.e., multiple entries in src
map to the same entry in dst
, according to the docs it is unspecified which of the source entries will be written to dst
:
For advanced assignments, there is in general no guarantee for the iteration order. This means that if an element is set more than once, it is not possible to predict the final result.
If we are additionally given a precedence array
p = np.random.rand(*src.shape)
how can we use p
to disambiguate this situation, i.e., write the entry with highest precedence according to p
?