n = 1000 c = 20 a = np.random.rand(n,n) a[np.diag_indices_from(a)] /= c # 119 microseconds a.flat[::n+1] /= c # 25.3 microseconds
#取出off diagonal的方法
np.delete(a, a.ravel()[::n+1])