学个p-np.triu(m, k),np.tirl()

np.triu(m, k),取矩阵a的上三角数据。斜线开始列位置由k确定

Signature: np.triu(m, k=0)
Docstring:
Upper triangle of an array.

Return a copy of an array with the elements below the k-th diagonal
zeroed.

Please refer to the documentation for tril for further details.

See Also
--------
tril : lower triangle of an array

Examples

np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)

>>>array([[ 1, 2, 3],

​ [ 4, 5, 6],

​ [ 0, 8, 9],

​ [ 0, 0, 12]])

np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], 1)

>>>array([[0, 2, 3],

​ [0, 0, 6],

​ [0, 0, 0],

​ [0, 0, 0]])

np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], 0)

>>>array([[1, 2, 3],

​ [0, 5, 6],

​ [0, 0, 9],

​ [0, 0, 0]])

np.tirl(),取矩阵a的下三角数据

  • 整理自网络
posted @ 2021-07-05 10:15  地球修者  阅读(116)  评论(0编辑  收藏  举报