, , , , , np.triu_indices :
def my_triu_indices(n, k=0):
rows, cols = np.triu_indices(n, k)
rows = cols - rows - k
return rows, cols
:
>>> a = np.array([2,3,4,5,6,7])
>>> b = np.zeros((4, 4), dtype=a.dtype)
>>> b[my_triu_indices(4, 1)] = a
>>> b
array([[0, 2, 5, 7],
[0, 0, 3, 6],
[0, 0, 0, 4],
[0, 0, 0, 0]])
>>> b[my_triu_indices(4, 1)]
array([2, 3, 4, 5, 6, 7])