Suppose I have a 3D array, how can I fill the diag of the first two dimensions to zero. For example
JavaScript
x
4
1
a = np.random.rand(2,2,3)
2
for i in range(3):
3
np.fill_diagonal(a[:,:,i], 0)
4
Is there a way to replace the for loop?
Advertisement
Answer
The following is one of the solution
JavaScript
1
3
1
a = np.random.rand(2,2,3)
2
np.einsum('iij->ij',a)[ ] = 0
3