The problem I want to solve in a preferably numpythonic way is this: I have a list A of 2d indices, for example:
A = [(0, 3), (2, 2), (3, 1)]
My goal is to now get an array
[[H H H 0],
[H H H H],
[H H 1 H],
[H 2 H H]]
Where H would be some default value (for example -1) So the problem is generally about inverting an array in this fashion.
If A is injective (no value appears twice) I can state it rigorously:
Let A be an injective array of 2d-indices. Then, generate a 2d-array B such that B[i, j] = A.index((i, j))
Or for A not necessarily injective:
Let A be an injective array of 2d-indices. Then, generate a 2d-array B such that A[B[i, j]] = (i, j)
More specifically in the non injective case we could resolve the situation with an additional "decider" function. Say
A = [(0, 3), (2, 2), (3, 1), (0, 3)]
Then to resolve the conflict between (0, 3) being in position 0 and 3, I would like to apply some function to equivalent indices to find a definite value.
As an example: In my case, specifically, I have a second array C with the same length as A. If there are several candidates (2d-indices) in A for one "position" in the final 2d array, the chosen one should be the one whose 1d index in A minimizes the value in C.
I hope the problem is made clear by these examples. Thank you for any help.
Edit: more example:
A = [(0, 3), (2, 2), (3, 1)]
print(my_dream_func(A, default=7)
>>> [[7 7 7 0],
[7 7 7 7],
[7 7 1 7],
[7 2 7 7]]
A = [(0, 3), (2, 2), (3, 1), (0, 3)]
print(my_dream_func(A, default=7))
>>> Err: an index appears twice
an alternative for this scenario:
def resolveFunc(indices):
c = [0.5, 2.0, 3.4, -1.9]
return(np.argmin(c[indices]))
A = [(0, 3), (2, 2), (3, 1), (0, 3)]
print(my_dream_func(A, resolveFunc, default=7))
#now resolveFunc is executed on 0 and 3
#because 0.5 > -1.9, 3 is chosen as the value for (0, 3)
>>> [[7 7 7 3],
[7 7 7 7],
[7 7 1 7],
[7 2 7 7]]