#### Previous topic

scipy.sparse.tril

#### Next topic

scipy.sparse.bmat

# scipy.sparse.triu

`scipy.sparse.``triu`(A, k=0, format=None)

  Return the upper triangular portion of a matrix in sparse format

  Returns the elements on or above the k-th diagonal of the matrix A.
    - k = 0 corresponds to the main diagonal
    - k > 0 is above the main diagonal
    - k < 0 is below the main diagonal

  Parameters:

  A : dense or sparse matrix

  > Matrix whose upper trianglar portion is desired.

  k : integer

  > The bottom-most diagonal of the upper triangle.

  format : string

  > Sparse format of the result, e.g. format=”csr”, etc.

  Returns:

  L : sparse matrix

  > Upper triangular portion of A in sparse format.

  See also

  `tril`
    lower triangle in sparse format

  Examples

      >>> from scipy.sparse import csr_matrix, triu
      >>> A = csr_matrix([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
      ...                dtype='int32')
      >>> A.toarray()
      array([[1, 2, 0, 0, 3],
             [4, 5, 0, 6, 7],
             [0, 0, 8, 9, 0]])
      >>> triu(A).toarray()
      array([[1, 2, 0, 0, 3],
             [0, 5, 0, 6, 7],
             [0, 0, 8, 9, 0]])
      >>> triu(A).nnz
      8
      >>> triu(A, k=1).toarray()
      array([[0, 2, 0, 0, 3],
             [0, 0, 0, 6, 7],
             [0, 0, 0, 9, 0]])
      >>> triu(A, k=-1).toarray()
      array([[1, 2, 0, 0, 3],
             [4, 5, 0, 6, 7],
             [0, 0, 8, 9, 0]])
      >>> triu(A, format='csc')
      <3x5 sparse matrix of type '<type 'numpy.int32'>'
              with 8 stored elements in Compressed Sparse Column format>