I masked a sorted 1-D numpy array using the method below (which follows a solution proposed here):
def get_from_sorted(sorted,idx):
mask = np.zeros(sorted.shape, bool)
mask[idx] = True
return sorted[mask]
The python method returns the array after masking on the indexes idx
. For example, if sorted=np.array([0.1,0.2,0.3.0.4,0.5])
, and idx=np.array([4,0,1]
), then the method get_from_sorted
should return np.array([0.1,0.2,0.5])
(note the order in the original array is preserved.)
Question: I need to get the mapping between the indices of the items in the array after masking and those in the original list. In the example above, such a mapping is
0 -> 0
1 -> 1
2 -> 5
because 0.1, 0.2, and 0.5 is on the 0th, 1st, and 5th place in sorted
.
How can I program this mapping efficiently?
Requirement on efficiency: Efficiency is the key in my problem solving. Here, both "idx" and "sorted" is a 1-D array of 1 million elements, and idx is a 1-D array of about 0.5 million elements (taken from an image processing application). Thus, checking the elements of the masked array one by one, or in a vectorized fashion, against the original array, for example, using np.where, would not perform well in my case. Ideally, there should be a relatively simply mathematical relation between the indices in the masked array and the original sorted array. Any idea?
I assume (from your example) that the original list is the sorted list. In which case, unless I misunderstand, you just do:
and then the mapping is
i-> idx[i]
Of course, if the original order of
idx
is important, make a copy first.