Suppose I have a matrix v:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
I also have a vector col_indices = [0,0,1,2,2,1] that indicates which column I should put an 1 for each row of matrix v.
The result of the task, in this case, should be:
1 0 0
1 0 0
0 1 0
0 0 1
0 0 1
0 1 0
The following code works:
v[np.arange(v.shape[0]), col_indices] = 1
But I was wondering if there is a clever way to accomplish this, because in the code above I have to create a vector just to index the matrix and that seems wasteful.
I have also tried the following code, but it doesn't do what I want:
v[:, col_indices] = 1