1

I have a 2D array 'A' with the shape (1015, 1200). Variable L1 has the lower indices and L2 has higher indices. On every one of the 1015 rows in A, I want to change the values of the elements between L1 and L2 pertaining to that row. I am using a for loop to carry this operation now and I want to know if there is any better way.

Here is the code that I am using now:

for i in range(A.shape[0]):
    A[i, L1[i]:L2[i]] = 0

Thank you.

1 Answers1

2

One thing you could try if you want it to be vectorized is

x = np.arange(A.shape[1])
mask = (x >= L1[:, np.newaxis]) & (x < L2[:, np.newaxis])
A[mask] = 0

Although, I'm not sure if it'll be faster and it definitely uses a lot more memory.

Roy Smart
  • 664
  • 4
  • 12