Suppose A and B are two 1d arrays of different sizes such that pythonically B = A[C] where C is a particular list of indices meeting some specific but otherwise known condition. How to get C if A and B are both known? I tried C = np.where(np.close(A, B))
but I get the following error message:
File "/home/username/../my_script.py", line 897, in get_histogram
i0, i1 = np.where(np.isclose(hist_xs, vals1)), np.where(np.isclose(hist_ys, vals2))
File "<__array_function__ internals>", line 6, in isclose
File "/usr/local/anaconda3/lib/python3.7/site-packages/numpy/core/numeric.py", line 2260, in isclose
return within_tol(x, y, atol, rtol)
File "/usr/local/anaconda3/lib/python3.7/site-packages/numpy/core/numeric.py", line 2246, in within_tol
return less_equal(abs(x-y), atol + rtol * abs(y))
ValueError: operands could not be broadcast together with shapes (722,) (1536,)
In other words, I am trying to get only those elements of A whose right indices would correspond to the known B array.
Are you looking for this?:
sorti = np.argsort(A)
C_inv = sorti[np.searchsorted(A,B,sorter=sorti)]
sample code (it works on any sortable array, if your array elements do not have comparison operator, you could write your own. If (less/greater) comparison is not applicable, you would need a for loop to find elements which seems to easy to include here):
A: [89 28 86 73 29 71 37 46 15 52]
B: [86 52 15]
C: [2 9 8]
C_inv: [2 9 8]