Issue
Consider the code
import numpy as np
v = np.linspace(0, 9, 10)
w = np.array([3.5, 4.5])
idx = np.searchsorted(v, w)
v = np.insert(v, idx, w)
print(idx, v[idx])
which outputs
[4 5] array([3.5, 4. ])
The variable idx
contains the indices of the elements of w
if they were inserted in v
one by one. When inserting an array into another like above, only the value of idx
corresponding to the minimum value of w
will give as well the position of the same value into v
.
Is there a way with numpy functions to obtain the indices of the elements of w
once inserted?
Solution
One solution:
import numpy as np
v = np.linspace(0, 9, 10)
w = np.array([3.5, 4.5])
idx = np.searchsorted(v, w)
v = np.insert(v, idx, w)
new_idx = idx + np.arange(len(idx))
print(new_idx, v[new_idx])
Output
[4 5] [3.5 4.5]
Answered By - Dani Mesejo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.