Reproducible setup
I have an array of n pairs of indices:
indexarray=\
np.array([[0,2,4,6],
[1,3,5,7]])
I also have a 2D array:
zeros = np.zeros((10,9))
and a list of n values:
values = [1,2,3,4]
I would like to add each kth element from the values
list to the element in the zeros
list having indeces equal to the kth indices pair
A solution
# works, but for loop is not suitable for real-world use-case
for index, (row, col) in enumerate(indexarray.T):
zeros[row, col] = values[index]
Visualize what I get:
plt.imshow(zeros)
Results as expected.
How can I do this without iteration?
Similar but different questions:
- Index 2D numpy array by a 2D array of indices without loops : here the output is an array of pairs of indices, not a 2D array, as here.
- Fill 2D numpy array from three 1D numpy arrays : setup is more complicated, they create a 2D array from 1D arrays, unlike here, where we already have a 2D array
- numpy arrays: filling and extracting data quickly : way more complicated setup, do not start from 2D array
- How to convert List of Lists of Tuples- pairs (index,value) into 2D numpy array : aim is different and for loop is not avoided