I can change a set of values of a numpy matrix, passing the indices that I want to change in a list form, e.g. matrix[[some first indices], [some second indices]] = 1
I'm trying to do this but with only one list of indices and then for each element of that list as the first index and the whole list as the other index. Here the example:
import numpy as np
#Matrix
matrix = np.zeros((5,5))
# Indices
elements = [1,2,3]
# Way that works
for i in elements:
matrix[i, elements] = 1
I would like to do this as a list comprehension, but I cannot figure out how, and, also, I'm not sure if it is a good practice. Something like matrix[[(i,elements) for i in elements]] = 1
Also thinking about to do it with itertools, for example matrix[(itertools.permutation(elements, 2))] = 1. But any of those approaches work, and I'm not sure why.
matrix += 1