triu_indices

paddle. triu_indices ( row, col=None, offset=0, dtype='int64' ) [source]

Return the indices of the upper triangular part of the 2-D matrix whose row and col is known. Indices are ordered based on row and then columns. The upper triangular part of the matrix is defined as the elements on and above the diagonal.

Parameters
  • row (int) – The input x which is a int number describe the number of row of the matrix.

  • col (int, optional) – The input x which is a int number describe the number of col of the matrix. default value for col is None, then it will be set equal to row, indicting a square matix.

  • offset (int, optional) –

    The offset to consider, default value is 0.

    • If offset = 0, all elements on and above the main diagonal are retained.

    • If offset > 0, include just as few diagonals above the main diagonal.

    • If offset < 0, excludes just as few diagonals below the main diagonal.

  • dtype (str|np.dtype|paddle.dtype, optional) – the data type of the output tensor, can be int32, int64, default value is int64.

Returns

Results of the indices of upper triangular part of a row * col matrix, where the first row contains row coordinates of and the second row contains column coordinates.

Return type

Tensor

Examples

>>> import paddle
>>> # example 1, default offset value
>>> data1 = paddle.triu_indices(4,4,0)
>>> print(data1.numpy())
[[0 0 0 0 1 1 1 2 2 3]
 [0 1 2 3 1 2 3 2 3 3]]
>>> # example 2, positive offset value
>>> data2 = paddle.triu_indices(4,4,2)
>>> print(data2.numpy())
[[0 0 1]
 [2 3 3]]
>>> # example 3, negative offset value
>>> data3 = paddle.triu_indices(4,4,-1)
>>> print(data3.numpy())
[[0 0 0 0 1 1 1 1 2 2 2 3 3]
 [0 1 2 3 0 1 2 3 1 2 3 2 3]]