I have a matrix foo with n rows and m columns. Example:
>>> import numpy as np
>>> foo = np.arange(6).reshape(3, 2) # n=3 and m=2 in our example
>>> print(foo)
array([[0, 1],
[2, 3],
[4, 5]])
I have an array bar with n elements. Example:
>>> bar = np.array([9, 8, 7])
I have a list ind of length n that contains column indices. Example:
>>> ind = np.array([0, 0, 1], dtype='i')
I would like to use the column indices ind to assign the values of bar to the matrix foo. I would like to do this per row. Assume that the function that does this is called assign_function, my output would look as follows:
>>> assign_function(ind, bar, foo)
>>> print(foo)
array([[9, 1],
[8, 3],
[4, 7]])
Is there a pythonic way to do this?
foo[np.arange(len(foo)), ind] = bar?