python - Access matrix by list of matrix indices in Numpy -
i have list of matrix indices , want access matrix these indices.
example:
indices = [(2, 0), (2, 1), (2, 2)] mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] mat[indices] = 0
this should result in [[1, 2, 3], [4, 5, 6], [0, 0, 0]]
, unfortunately "list indices must integers, not list
".
edit
as user2357112 suggests in comment tried following now:
mat = numpy.array(mat) indices = numpy.array(indices) mat[indices] = 0
but unfortunately whole matrix filled 0 now.
indices
regular list of tuples , can't used element of regular mat
. can iterate on list indices:
for x, y in indices: mat[x][y] = 0
if want use numpy
methods, need create numpy array. indeed, np.array
structure allows use tuple access element:
mat = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) item in indices: mat[item] = 0
Comments
Post a Comment