0

After using np.instersect1d I have retrieved the indices that I want to retrieve. I want to turn these indices into a mask so I can then combine it with another mask.

What I am wanting:

>>> times_1.shape
... (160,)

>>> _, time_indices, _ = np.intersect1d(times_1, times_2, return_indices=True)

>>> time_indices.shape
... (145,)

    # Some way to turn return mask from original shape and indices
>>> time_mask = get_mask_from_shape(time_1.shape, time_indices)

>>> time_mask.shape
... (160,)

What would be the simplest way to implement the function get_mask_from_shape?

loki
  • 976
  • 1
  • 10
  • 22
Max Collier
  • 573
  • 8
  • 24

2 Answers2

1

The solution I've found is:

time_mask = np.zeros(times_1.shape, dtype=bool)
time_mask[time_indices] = True
Max Collier
  • 573
  • 8
  • 24
-1

If I get an idea right, I would apply this pattern I've been looking for a few days before:

intersection = np.intersect1d(times_1, times_2)
intersection = np.sort(intersection)
locations = np.searchsorted(intersection, times_1)
locations[locations==len(intersection)] = 0
time_mask = intersection[locations]==times_1

searchsorted method finds indices where elements of times_1 should be inserted into intersection to maintain order, according to numpy documentation.

mathfux
  • 5,759
  • 1
  • 14
  • 34