Suppose there is a sorted array a and a matrix b
a = np.array([11,30,60,70])
b = np.array([[11, 2, 30, 4],
[30, 60, 70, 5],
[1, 2, 3, 4]]
)
I could get the results like
results = np.array([[0, 2, 1, 4],
[1, 2, 3, 5],
[1, 2, 3, 4]]
)
It means searching each element from array a in matrix b and replace the founded one with the index in array a.
I know it is a bit complex, I have done it in a loop
def set_values_found_to_index(matrix_original, values_to_find):
"""Set value found to index."""
for index, elem in enumerate(values_to_find):
matrix_original[np.where(
values_to_find == values_to_find[index]
)] = index
return matrix_original
My question is there any chance to do it without a loop?
Updated: @NilsWerner code works perfectly regarding the above question, because there are still ambiguous questions about the apple and pear mix, so I update the question:
a = np.array([11,30,60,70])
b = np.array(
[[11, 2, 30],
[30, 60, 70],
[20, 30, 50],
[11, 30, 60],
[30, 11, 70],
[70, 11, 60],
[1, 2, 3]]
)
results = np.array(
[[1,2,3],
[0,1,2],
[1,0,3],
[3,0,2]]
)
It means based on the above question, the apple and pear mix problem can be solved by remove the lines which contain the elements either smaller than a.min(), bigger than a.max() or not in a.