Skip to content Skip to sidebar Skip to footer

Fill A Matrix From A Matrix Of Indices

I want to fill a matrix from an array of indices : import numpy as np indx = [[0,1,2],[1,2,4],[0,1,3],[2,3,4],[0,3,4]] x = np.zeros((5,5)) for i in range(5): x[i,indx[i]] = 1.

Solution 1:

Use advanced-indexing after intialization -

x[np.arange(len(indx))[:,None], indx] = 1

Post a Comment for "Fill A Matrix From A Matrix Of Indices"