Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:28:03+00:00 2026-06-04T18:28:03+00:00

Is there a function in numpy/scipy that returns all the elements of one of

  • 0

Is there a function in numpy/scipy that returns all the elements of one of the triangles (upper or lower) of a square matrix?

e.g.:

matrix = [[1,  2,  3],
          [4,  5,  6],
          [7,  8,  9]]

triangles (upper and lower):

up = [1,2,3,5,6,9]
down = [1,4,5,7,8,9]

or

up = [1,2,5,3,6,9]
down = [1,4,7,5,8,9]

Thank you!

EDIT:

Yes there are two functions that help you do that: np.triu_indices(n) (for the upper triangle) and np.tril_indices(n) (for the lower triangle).

Thanks katrielalex!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:28:04+00:00Added an answer on June 4, 2026 at 6:28 pm

    Does the order of the elements matter?

    Normally the upper and lower triangles of a matrix are taken about the other diagonal (top left to bottom right). To fix that, you need to “flip” between the two diagonals, which you can do with np.fliplr(matrix). That will get you the correct elements, but in the “natural” order (row-by-row, with each row getting shorter). You can also get the column-by-column order by flipping the other way (np.flipud). But I don’t know any way to get the “reading by smaller diagonals” order that you are using, short of reading the matrix one diagonal at a time.

    To get the diagonal elements you can get their indices with np.triu_indices (or, for the lower triangle, np.tril_indices) and then index by them.

    >>> matrix
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    >>> np.fliplr(matrix)[np.triu_indices(3)]
    array([3, 2, 1, 5, 4, 7])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a numpy function (or algorithm) that only returns the items of an
Is there any built-in function in Python3/Numpy which filters an array and returns indices
Is there a function in numpy that determines whether strings should be integers or
Is there a numpy function that gives for a given numpy array its maximum
Question: I've profiled my Python program to death, and there is one function that
Is there a numpy function to divide an array along an axis with elements
Is there a function in c# that takes two 32 bit integers (int) and
is there any function in C++ that calculates a fingerprint or hash of a
Is there a function that tells what class you are extending? Like function_exists? I
In NumPy functions, there are often initial lines that do checking of variable types,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.