Here is an example of the situation I want to find an efficient solution for:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([0,1,2])
The vector b should have one integer entry for each row of a, ranging between 0 and the number of columns of a. The desired output c is a matrix in the shape of a, with i-th row containing every entry up to (and excluding) the b[i]-th entry of i-th row of a, the rest of the row should be filled up with zeros. Hence, in the example given, we look for the following solution:
c = np.array([[0,0,0],[4,0,0],[7,8,0]])
There are two "easy" ways to do this:
c = np.zeros(a.shape)
for i in range(a.shape[0]):
c[i, :b[i]] = a[i, :b[i]]
another is by first defining an auxiliary matrix and consequently broadcasting it against a.
aux = np.zeros(a.shape)
for i in range(a.shape[0]):
aux[i, :b[i]] = 1
c = a * aux
What I am looking for are vectorized solutions that scale well in runtime when I increase the number of rows of a.