I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3))
example
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
Then, list b = [4,3,1]
determines the different range sizes for each column slice, so that we would get the arrays
[0 3 6 9]
[1 4 7]
[2]
which we can concatenate and flatten to get the final desired output
[0 3 6 9 1 4 7 2]
Currently, to perform this task, I am using the following code
slices = []
for i in range(a.shape[1]):
slices.append(a[:b[i],i])
c = np.concatenate(slices)
and, if possible, I want to convert it to a pythonic format.
Bonus: The same question but now considering that b
determines row slices instead of columns.