Given these arrays,
R = np.array([[ 5., 3., 2., 7., 3., 6., 8., 9., 10., 55.],
[ 5., 4., 2., 7., 3., 6., 8., 10., 10., 55.]])
F = np.array([[ 0.2 , 0.4 , 0.1 , 0.3 , 0.25, 0.25, 0.2 , 0.1 , 0.1 , 0.1 ],
[ 0.3 , -0.4 , 0.1 , 0.3 , 0.25, 0.25, 0.4 , -0.4 , 0.1 , 0.1 ]])
K = nparray([[2],
[1]])
I want to sort each row of F
and then find the first K[0]
indices of the first row and the first K[1]
indices of the second row of the sorted F
. Then using these indices, I want to add 1 to the elements of R
.
Here is my attempt, I was able to get the indices:
indices = np.argsort(F)[np.tile(np.arange(F.shape[1]),(F.shape[0],1)) < K]
# indices = np.array([7, 8, 7], dtype=int64)
But I am not sure how to vectorize the below operations without using a for loop.
Rnew = R.copy()
Rnew[0,7] =R[0,7]+1
Rnew[0,8] =R[0,8]+1
Rnew[1,7] =R[1,7]+1
Rnew = np.array([[ 5., 3., 2., 7., 3., 6., 8., 10., 11., 55.],
[ 5., 4., 2., 7., 3., 6., 8., 11., 10., 55.]])